Java Programming - Flow Control - Discussion

Discussion :: 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 ) ;
    }
}

[A]. I is 1
[B]. I is 1 I is 1
[C]. No output is produced.
[D]. Compilation error

Answer: Option C

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.


Pawan said: (Jul 22, 2010)  
Is do-while syntax correct? guess not.

Ashi said: (Sep 6, 2011)  
I guess there is a syntax error.

Srikar said: (Jan 26, 2012)  
More clarified equivalent of above program is:

do
{
while(i<1)
{
System.out.....;
}
}
while(i>1);

Jaics said: (Jun 16, 2013)  
while ( I < 1 )
System.out.print("I is " + I)

They form two statements, how can they be a single statement...So curly bracket is required..right?

Austin said: (Jul 31, 2013)  
The compiler will try to associate the 1st "while" encountered with the last "do" encountered.

Hence, it should give compilation error as statement missing;

Simar said: (Feb 23, 2015)  
From what I understand here is that expressions are not counted as statements. So technically there is only one statement i.e in the while (I < 1).

I could also add more expressions, and it would still be the same. Hope the following example helps you understand it better.

class Test
{
public static void main(String [] args)
{
int I = 1;
do for(int j = 0; j < 5; j++) while(I < 1)
System.out.print("I is " + I);
while ( I > 1 ) ;
}
}

Suresh said: (Mar 25, 2015)  
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 ) ;
}
}

As per my understanding above code inside do-while loop while expression is having only one statement that is reason there is no brackets required.

Raj said: (Aug 6, 2015)  
do while(); semicolon is required.

Zoran said: (Nov 5, 2015)  
Obviously this is the answer :)

public static void main(String [] args)
{
int I = 5;
do //start do while
while ( I < 10 ) System.out.print("I is " + I);//one statement
while ( I > 1 ) ; //end do while
}

Rahul Khandare said: (Apr 7, 2016)  
What does int I mean?
Can we declare variable name as a capital letter?
How compiler gets this as a token?

Ramakrishna said: (Jul 4, 2016)  
Answer should be option D. Compilation error due to unreachable code.

Vasanth said: (Aug 11, 2016)  
I Tried to run this program. Strangely this may run, Can someone explain how it works?

Rajat Bansal said: (Aug 27, 2016)  
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.

Mounika said: (Jun 14, 2021)  
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:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.