Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 14)
14.
What will be the output of the program?
int i = 0; 
while(1) 
{
    if(i == 4) 
    {
        break;
    } 
    ++i; 
} 
System.out.println("i = " + i);
i = 0
i = 3
i = 4
Compilation fails.
Answer: Option
Explanation:

Compilation fails because the argument of the while loop, the condition, must be of primitive type boolean. In Java, 1 does not represent the true state of a boolean, rather it is seen as an integer.

Discussion:
2 comments Page 1 of 1.

Vinit Nagap said:   6 years ago
while(1) is not an infinite loop in java like c, while(1) gives a compile-time error 1 should be replaced by TRUE.

Aayushkanungo said:   7 years ago
Here, the compilation fails as while(1) is always true and the loop will be in an infinite loop which JVM not supports.

Post your comments here:

Your comments will be displayed after verification.