Java Programming - Flow Control - Discussion

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

5. 

What will be the output of the program?

public class If2 
{
    static boolean b1, b2;
    public static void main(String [] args) 
    {
        int x = 0;
        if ( !b1 ) /* Line 7 */
        {
            if ( !b2 ) /* Line 9 */
            {
                b1 = true;
                x++;
                if ( 5 > 6 ) 
                {
                    x++;
                }
                if ( !b1 ) 
                    x = x + 10;
                else if ( b2 = true ) /* Line 19 */
                    x = x + 100;
                else if ( b1 | b2 ) /* Line 21 */
                    x = x + 1000;
            }
        }
        System.out.println(x);
    }
}

[A]. 0
[B]. 1
[C]. 101
[D]. 111

Answer: Option C

Explanation:

As instance variables, b1 and b2 are initialized to false. The if tests on lines 7 and 9 are successful so b1 is set to true and x is incremented. The next if test to succeed is on line 19 (note that the code is not testing to see if b2 is true, it is setting b2 to be true). Since line 19 was successful, subsequent else-if's (line 21) will be skipped.


Isha said: (Jun 14, 2011)  
What is the meaning of operator | ? please do reply.

Chibhulpandey said: (Sep 23, 2011)  
I think it is OR operator!! but actly or operator should be represented as"||". So please modify it in program.

Sam said: (Nov 11, 2012)  
int z =5;
if(++z > 5 || ++z >6)
z++;
output : z=7
________________________
int z =5;
if(++z > 5 | ++z >6)
z++;
output : z=8

Saad said: (Jan 30, 2013)  
If(b==true) is equivalent to if(b).

But if(b=true) first assigns true value to b and then check the condition.

Rakesh Kumar said: (Feb 5, 2013)  
How it became possible for this code to run.

Because there must be an else statement after if{} else if{}.

Please some one do reply if I am wrong.

Rakesh said: (Feb 5, 2013)  
|| is called as short circuit OR operator, it will check its 2nd operand only if the first operand is false and if the first operand is true then it will not care about whether 2nd operand is true or false.

| is simple OR operator it will check its both the operands always.

Sunil said: (Oct 5, 2013)  
How come b2 become true ?

Vien said: (Jan 16, 2014)  
Why? x must be 1001, why 101?

Kunal said: (Aug 22, 2014)  
Note the difference between b2= true and b==true. The first one assigns b2 to be true and evaluates as true, the second one compares b2 with true.

As the condition is true@ if(b2=true) it will executes the statement x=x+100; and skip the next statement.

Boopathi said: (Jun 17, 2015)  
Why not consider this part? Please explain.

if(5>6)
{
x++;
}
if(!b)
{
x=x+10;
}

Pandu said: (Jul 24, 2015)  
Can we assign a value to a variable in if condition that mean if (x=5). Is it applicable or not?

Raman said: (Aug 23, 2015)  
If (!b2) //If I change b2 to b1 then why execute this loop? Please

x = x+10;

else if (b1 = true) /*Line 19*/.

x = x + 100;
else if (b1 | b2) /*Line 21*/
x = x + 1000;

Udesha said: (Sep 28, 2015)  
What is the meaning of "if (!b1)"? What does it do?

Don't we have to use a condition inside those parentheses of the if condition?

Yash Maheshwari said: (Oct 15, 2015)  
Can any one explain it step by step?

Divesh Singla said: (Oct 26, 2015)  
if ( 5 > 6 )
{
x++;
}
if ( !b1 )
x = x + 10;

Why this part is not considered?

Devyansh Singla said: (Oct 26, 2015)  
@Boopathy according to my understanding.

If (5 > 6) false 5 is less than 6 so it will move to next if where b1 is already true above so it will move to last loop.

{
x++;
}
if ( !b1 )
x = x + 10;

Sujala said: (Feb 17, 2016)  
If(!b2) returns true.

It is similar to !(false).

In condition if (b2 = true) at this moment true is assigned to b2 and returns true value.

But we can't able to assign if (i = 5) //it is error.

At any instance if condition returns either true or false only.

So the statements.

x = x+100; //x = 1+100.

x = 101 is the right answer.

Geeta said: (Jun 20, 2016)  
A question is wrong. Kindly correct it at.

X = x + 1000.

Kishan said: (Jul 3, 2016)  
x must be 1001, assignment is not allowed inside an if statement in Java .

Yami said: (Nov 16, 2016)  
This will compile? Explain.

"else if (b2 = true) /* Line 19 */ ".

b2 = true ?

Arun said: (Jan 23, 2017)  
In this statement, if(b2=true) first assign b2 value as true then check condition.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.