Java Programming - Flow Control - Discussion

Discussion :: Flow Control - General Questions (Q.No.3)

3. 

public void test(int x) 
{ 
    int odd = 1; 
    if(odd) /* Line 4 */
    {
        System.out.println("odd"); 
    } 
    else 
    {
        System.out.println("even"); 
    } 
}
Which statement is true?

[A]. Compilation fails.
[B]. "odd" will always be output.
[C]. "even" will always be output.
[D]. "odd" will be output for odd values of x, and "even" for even values.

Answer: Option A

Explanation:

The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.


Akash said: (Mar 27, 2011)  
I am confused about it. At 4th Line if expects any value if it's equal to 0 than compilation will go in if condition otherwise else condition.

Jaladi said: (Apr 15, 2011)  
Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.

Chandra Bhushan said: (Apr 10, 2012)  
If condition take two value 1 and 0 ant 1=true 0=false.
Now I is int value which contain value 1 so it should be true.

Prashant said: (Nov 26, 2012)  
Here if (odd) expects value of odd variable.

As declared earlier value of odd is 1 then condition holds true. Odd should be printed. Didn't understand why it is compiler error.

Manoj said: (Apr 7, 2013)  
Here if (odd) expects value of odd variable.

As declared earlier value of odd is 1 then condition holds true. Odd should be printed. Didn't understand why it is compiler error.

Surajit said: (Jan 30, 2014)  
In C programming language while(1){} is a infinite loop. But in java while(1){} is a compilation error because in condition it expects either true of false(both keyword). So, the same c program equivalent code in java is while(true){} which represents infinite loop.

Bharathraj said: (Oct 12, 2014)  
How it is a compile error?

Bandana said: (Oct 14, 2014)  
In line 4. There is no Boolean condition. It just an integer. That's why it is compile error.

Nashath Nasar said: (Jan 1, 2015)  
Hi guys,

Option A is the correct answer, because there should be a Boolean expression within the brackets followed by "if". Here is an integer number (Odd) within the brackets. That's why it's compile error.

Anjan Chandra Das said: (Aug 18, 2015)  
In java the condition parameters are "true" and "false" i.e. in Boolean format.

But in C or C++ the positive integers are taken as true like.

If (500) // is taken as true in C language. So, Don't be confuse the feature of java and C.

Shantanu said: (Aug 24, 2015)  
What when we use while (1)?

That time it gives exact output in c++. It is not allowed in java.

Shrek said: (May 28, 2016)  
As far as I have learned, any number other than 0 in the if condition results in a true result. 0 returns false and hence in this case if should actually return true and print ODD.

Although, I am not sure if this is invalid incase of java.

Surendra said: (Nov 12, 2016)  
With in if condition boolean expression is given.

Sajib said: (Jun 18, 2017)  
How it becomes compilation error? 0 if false otherwise true.

Vikash Rai said: (May 6, 2019)  
Integer can not behave like boolean. And there is not a complete condition statement in if condition.

So it will throw compilation error.

Thanks.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.