How Does a for Loop Work, Specifically For(;;)

How does a for loop work, specifically for(;;)?

A for loop in java has the following structure -

for (initialization statement; condition check; update)
loop body;

As you can see, there are four statements here -

  1. Initialization statement: This statement is executed only once, when the loop is entered for the first time. This is an optional statement, meaning you can choose keep this field blank. It is generally used for some initialization purpose.
  2. Conditional check: This statement is probably the most important one. It checks to verify whether or not certain expression evaluates to true. If it is, then the loop execution continues. You can choose to keep this field empty, which will be evaluated to true.
  3. Update: This statement list is executed from left to right, typically used to increment/decrement some variable.
  4. loop body: The body of the loop, which will be executed again and again based on the conditional check's truth value.

Basically this is how the execution follows - first, when the loop is entered for the first time, the initialization statement is executed once. Then the conditional check is executed to see if it evaluated to true. If it is, then the the loop body is executed, otherwise the loop execution is finished. After that, the Update statement(s) is(are) executed. Next, the conditional check is executed again, and if it evaluates to true, then again the loop body is executed, then update statement is executed, then again the conditional check....you get the picture.

Now about your for( ; ; ) syntax. It has no initialization statement, so nothing will be executed. Its conditional check statement is also empty, which means it evaluates to true after that the loop body is executed. Next, since the update statement is empty, nothing is executed. Then the conditional check is performed again which will again evaluates to true and then this whole process will again repeat.

So you see, this is basically an infinite loop which has no initialization statement, whose conditional check will always evaluates to true, and which has no update statement. This is equivalent to -

while(true)
{
.....
}

which is another popular loop construct in java.

When you use an infinite loop like this, it's important pay attention to the breaking condition as in most cases you can't let a loop to run indefinitely. To break out of these kinds of loops, you can use the break statement. The structure is as follows -

if(some_condition_is_true)
break; // This will cause execution to break out of its nearest loop

or

if(some_condition_is_false)
break;

How does the for loop exactly work out

A for loop works as follows:

  1. Initialization is done (int i=0 in your case; only executed once)
  2. Condition is checked (i<=100 here), if condition is false leave the loop
  3. Code within the braces is executed (System.out.println(i); in your case)
  4. Update statement is executed (i++)
  5. Goto 2.

How does a variable as a second statement in a for-loop work?

In the second statement of the for loop, you need to provide a condition on which the loop should run or stop. So, it should be something that gives an output of either true or false.

In this case, node will have some value. That value is evaluated as true if it is anything other than 0 or null. So, when the node.rest returns null. The condition will become false. Hence, stopping the loop.

for loops in Python - how to modify i inside the loop

for loops operate on iterables. In for i in range(0, lenDF), i is assigned the next value in the range on each round of the loop regardless of how it is used in the loop. The question then, is whether there is a clean way to write an iterable that does what you want. In this case, all you want is to advance by a fixed step and adjust the final step length to account for the end of data.

endRw=5
lenDF=97 # 1160

for i in range(0, lenDF, endRw):
endIndx = min(i+endRw, lenDF)
print("Range to use: ", i, ":", endIndx)

How does this specific nested for loop work in vanilla JavaScript?

Add a console after second for, you should see the visualization

let sum = 0;
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 2; j++) {
console.log(`sum=${sum} i=${i} j=${j}`)
sum = sum + i + j;
continue;
}
}
console.log(sum);

//output
// sum=0 i=0 j=0
// sum=0 i=0 j=1
// sum=1 i=1 j=0
// sum=2 i=1 j=1
// sum=4 i=2 j=0
// sum=6 i=2 j=1
// sum=9 i=3 j=0
// sum=12 i=3 j=1
// sum=16 i=4 j=0
// sum=20 i=4 j=1
// 25

Why use a for loop instead of a while loop?

In your case, you don't gain much besides one less line of code in the for loop.

However, if you declare the loop like so:

for(int i = 0; i < x; i++)

You manage to keep i within the scope of the loop, instead of letting it escape to the rest of the code.

Also, in a while loop, you have the variable declaration, the condition, and the increment in 3 different places. With the for loop, it is all in one convenient, easy-to-read place.

Last thought:
One more important note. There is a semantic difference between the two. While loops, in general, are meant to have an indefinite number of iterations. (ie. until the file has been read..no matter how many lines are in it), and for loops should have a more definite number of iterations. (loop through all of the elements in a collection, which we can count based on the size of the collection.)

What is the real difference between for loop and while loop?

for is just a syntactic sugar for while, if not to say both can be implemented with goto statement as well as many other language constructs.

The historical reason was to make your program code look more human readable.



Related Topics



Leave a reply



Submit