Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing while loops to for loops

I'm trying to change this while loop into a for loop and I'm not getting particularly far. I am new ish to programming, so apologies if this is a trivial task.

while (read < fileBytes.length
       && (numRead = diStream.read(fileBytes, read, fileBytes.length - read)) >= 0)
{
    read = read + numRead;
}
like image 317
OrangeSubmarine121 Avatar asked Dec 05 '25 09:12

OrangeSubmarine121


2 Answers

Try to use this :

start_value = //....
for (read = start_value; read < fileBytes.length 
        && (numRead = diStream.read(fileBytes, read, fileBytes.length - read)) >= 0; 
        read += numRead) {

    //Your actions

}
like image 168
YCF_L Avatar answered Dec 06 '25 23:12

YCF_L


I'm a C# Programmer, but I will write code which is very similar to what you need.

for (int read = 0; read < fileBytes.length;) {
    numRead = diStream.read(fileBytes, read, fileBytes.length - read);
    if (numRead >= 0) {
        read = read + numRead;
    }
}
like image 42
Mohammed Avatar answered Dec 06 '25 23:12

Mohammed