Java Programming - Flow Control - Discussion
Discussion Forum : 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);
Answer: Option
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.
Discussion:
8 comments Page 1 of 1.
Dev said:
1 decade ago
How 1, 10 nd 5, 6 come I can't understand. Can you please explain me?
Naina Dwivedi said:
1 decade ago
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.
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:
1 decade ago
Please explain properly if i=1 & j=9 then how to 1 is greater then 9?
Nashath Nasar said:
1 decade ago
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'.
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:
1 decade ago
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).
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:
9 years ago
while(++i<5), how the value will be i=5.
Gopi said:
7 years ago
Please explain, how j=6?
Cherupally Indu said:
2 years ago
I guess the iteration starts from i=2 and j=9 in the while loop.
so,
it is
2 9
3 8
4 7
5 6
And the answer is 5 6.
so,
it is
2 9
3 8
4 7
5 6
And the answer is 5 6.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers