Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 8)
8.
What will be the output of the program?
public class Test 
{
    public static void main(String [] args) 
    {
        int I = 1;
        do while ( I < 1 )
        System.out.print("I is " + I);
        while ( I > 1 ) ;
    }
}
I is 1
I is 1 I is 1
No output is produced.
Compilation error
Answer: Option
Explanation:

There are two different looping constructs in this problem. The first is a do-while loop and the second is a while loop, nested inside the do-while. The body of the do-while is only a single statement-brackets are not needed. You are assured that the while expression will be evaluated at least once, followed by an evaluation of the do-while expression. Both expressions are false and no output is produced.

Discussion:
14 comments Page 2 of 2.

Ramakrishna said:   9 years ago
Answer should be option D. Compilation error due to unreachable code.

Vasanth said:   9 years ago
I Tried to run this program. Strangely this may run, Can someone explain how it works?

Rajat Bansal said:   9 years ago
code can be write as:

do
{
while(I>1)S.O.P("I is :"+I);
}
while(I<1);

As 1 statement ends with the encounter of; which is present at the end of the print statement that's why there is no need of braces after "do" . and both conditions are false that's why no output.
(2)

Mounika said:   4 years ago
In the definition of the block, if we don, t specify statement using block ({}), only the first statement consider. Here condition file so no output produced.


Post your comments here:

Your comments will be displayed after verification.