Java Programming - Flow Control - Discussion

Discussion :: Flow Control - Finding the output (Q.No.2)

2. 

What will be the output of the program?

int i = 1, j = 10; 
do 
{
    if(i > j) 
    {
        break; 
    } 
    j--; 
} while (++i < 5); 
System.out.println("i = " + i + " and j = " + j);

[A]. i = 6 and j = 5
[B]. i = 5 and j = 5
[C]. i = 6 and j = 4
[D]. i = 5 and j = 6

Answer: Option D

Explanation:

This loop is a do-while loop, which always executes the code block within the block at least once, due to the testing condition being at the end of the loop, rather than at the beginning. This particular loop is exited prematurely if i becomes greater than j.

The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and then tests the loop condition, where a pre-incremented by one i is tested for being lower than 5. The test is at the end of the loop, so i can reach the value of 5 before it fails. So it goes, start:

1, 10

2, 9

3, 8

4, 7

5, 6 loop condition fails.


Dev said: (Nov 1, 2011)  
How 1, 10 nd 5, 6 come I can't understand. Can you please explain me?

Naina Dwivedi said: (Aug 19, 2012)  
Explanation of answer is wrong in first iteration of the loop. i value is 1 and j value is 9 and so forth loop will be go on till the time loop's condition don't generate false.
so value of in each loop will be:
i=1, j=9;
i=2, j=8;
i=3, j=7;
i=4, j=6;
i=5, j=6; // j's value don't change because the at this point when i is equal to 5 will return false value which breaks the loop.
Answer is correct but given explanation is wrong.

Pooja said: (Nov 11, 2014)  
Please explain properly if i=1 & j=9 then how to 1 is greater then 9?

Nashath Nasar said: (Jan 1, 2015)  
Hi @Pooja,

Initial value of I and j is 1, 10.

1 > 10 it's an false expression within the brackets followed by "if". So 'if' statement is not going to execute. After that the statement 'j--' is going to execute. Then 'j' is going to take the value 9.

After that final while loop going to execute. Here '++i' is within the brackets, so I is going to take the value 2. But "2 < 5" so the Expression within the while loop brackets is true. So the code going to run again same like this which I explained initially.

Like this the code going to run again and again until the Boolean expression in while loop is to be false. At a time 'i' will get the value of 5 and 'j' will get the value of 6. On that time 'i > 5' this expression going to be a false expression. Then the code will going to stop the run. So final answer is 'i = 5 and j = 6'.

Neeraj said: (Feb 23, 2015)  
Here i is pre increment,

So, After executing this statement.

while (++i < 5); i should be incremented by 1 first than it should be used in following SOP statement:

System.out.println ("i = " + i + " and j = " + j);

So according to me at after first loop, o/p should be like this:

i j.
-----.
2 10 (as j is post increment: first decrement, than use).

Bikash said: (Mar 12, 2016)  
while(++i<5), how the value will be i=5.

Gopi said: (Apr 29, 2018)  
Please explain, how j=6?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.