Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a for loop check its conditions in Java?

Tags:

java

for-loop

My question has to do with the order in which java checks the conditions of a for loop when there is a print statement in the "conditions" of the loop. It seems like an unpractical thing to do (I haven't ever seen it used in any practical way), though my lack of understanding of what is printed has me thinking that I may not fully understand how a for loop functions. The following question showed up on a recent exam:

What will the following method print with an input of n = 5?

public static void mystery(int n) {
    for (int i = -1; i < n; System.out.print(i + " ")) {
         i++;
    }
}

The correct answer is: 0 1 2 3 4 5

To me, it seems that the loop ought to print -1, then increment i by 1, print 0 ..... until i = 4. Then it would print 4, increment i by 1, and break out of the loop at the loop's condition i < n. Why is the correct answer what it is and why is my logic flawed?

like image 690
itscharlieb Avatar asked Dec 08 '13 04:12

itscharlieb


People also ask

DO FOR loops check condition first?

While loops check for the stopping condition first, and may not execute the body of the loop at all if the condition is initially false.

What is loop condition in Java?

The for loop in Java is an entry controlled loop that allows a user to execute a block of a statement(s) repeatedly with a fixed number of times on the basis of the test expression or test-condition.

Does for loop need a condition?

Optional for expressions All three expressions in the head of the for loop are optional. Like the initialization block, the condition block is also optional. If you are omitting this expression, you must make sure to break the loop in the body in order to not create an infinite loop. You can also omit all three blocks.


1 Answers

for(initialization; Boolean_expression; update)
{
   //Body
}

Here is the flow of control in a for loop:

  1. The initialization step is executed first, and only once.

  2. Next, the Boolean expression is evaluated.

  3. If Boolean expression is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop means loop is over.

  4. After the body of the for loop executes, the flow of control jumps back up to the update statement. Here your print statement is getting executed.

  5. The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop terminates.

For you below are the steps

in 1st step value of i is -1 at start.
in 2nd step i=-1 is less then n=5 so body will be executed.
in 3rd step i will be incremented to i=0
in 4th step value of i ( which is 0 gets printed)
in 5th step Boolean expression is evaluated again and it returns true as i=0 is less then n=5. so again step 3 ( Body of loop) is executed.
like image 94
Vipin Avatar answered Sep 18 '22 20:09

Vipin