Java Programming - Flow Control - Discussion

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

9. 

What will be the output of the program?

int x = l, y = 6; 
while (y--) 
{
    x++; 
} 
System.out.println("x = " + x +" y = " + y);

[A]. x = 6 y = 0
[B]. x = 7 y = 0
[C]. x = 6 y = -1
[D]. Compilation fails.

Answer: Option D

Explanation:

Compilation fails because the while loop demands a boolean argument for it's looping condition, but in the code, it's given an int argument.

while(true) { //insert code here }


Laxman.1249@Gmail.Com said: (Jul 22, 2011)  
I don't know this answer. Please explain the answer.

Praveen said: (Sep 18, 2011)  
While loop must have boolean type argument such as comparison.

Laasya said: (Sep 15, 2014)  
While loop and if condition must have Boolean type argument that is either true or false but here there is no Boolean value it is a decrement operator.

Rahul Jain said: (Dec 8, 2014)  
while(1)
{
//infinite loop, having int argument then why not this?
}

Aroosh said: (Dec 17, 2014)  
In Java while takes only Boolean values so seen while (1) will not work.

Mentalfrostbyte said: (Jan 8, 2016)  
I knew it was compilation error because the problem states that x equals the letter 'l', not the number '1'.

Ankita said: (Mar 23, 2017)  
Please explain this with the example.

Please help me with this.

Sarvepalli Madhu Babu said: (Jun 20, 2017)  
Anyone explain it with one example.

Jain said: (Oct 15, 2019)  
While can also takes condition too but as you said it only takes true argument ?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.