# Please exlain this code

Supriya Nimakuri
Ranch Hand
Posts: 83
Can anyone telll me wts the function of (!b1) and (!b2) in the following code.what will be value of 'b1' and 'b2' [ i guess they are flase ..since they are instance variables]. Also how this is working in for loop.

Also can you tell me what this statement performs else if(b1 | b2). This seems to be basic operator questions..but i was a bit confused

public class Example1 {

static boolean b1,b2;
public static void main(String[] args) {
int x=0;
if(!b1){
if(!b2){
b1 = true;
x++;
if(5>6){
x++;
}
if(!b1) x= x+10;
else if(b2= true)x=x+100;
else if(b1 | b2)x=x+1000;
}
}
System.out.println("Value of X:" +x);
}
}

wise owen
Ranch Hand
Posts: 2023

Value of X:101

Keith Lynn
Ranch Hand
Posts: 2409
The ! operator is a unary operator that changes the truth value of its operand.

Since b1 and b2 are not given explicit values, they are initialized to false.

When you use | between two boolean expressions, then both expressions are evaluated, and if either of them is true, the result is true.

Supriya Nimakuri
Ranch Hand
Posts: 83
Hai keith...

Thnks for explanation...

I got a small doubt here...b1 is assigned as false..but with what value is this compared in if statement -- if (!b1) -- does this mean tht if b1 is not false

Sreeram Desigan
Greenhorn
Posts: 23
if(b1)
{
// statements
}
here the statements will be executed only if b1 is true.
if(!b1)
{
//statements
}
here the statement will be executed only if b1 is false.
read if(!b1) as "if not b1" u can understand the meaning.

Supriya Nimakuri
Ranch Hand
Posts: 83
Thanks to Sreeram Desigan , Keith and Wise for expllaining the code

vga dolfus
Greenhorn
Posts: 1
if (b2=true)
should be written
b2 = true; // now you can write, why you set b2 to true! aha!
if (b2)

coding can be nice and easy.

wise owen
Ranch Hand
Posts: 2023

It is a trick in Exam test.