java. The output is 101. Can you explain me the following code? public class IfS
ID: 3743398 • Letter: J
Question
java. The output is 101. Can you explain me the following code?
public class IfStatement
{
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(x);
}
}
Explanation / Answer
public class IfStatement {
static boolean b1, b2;
public static void main(String[] args) {
int x = 0;
if (!b1) { // pass
if (!b2) { // pass
b1 = true;
x++; // x will be 1
if (5 > 6) { // fails
x++;
}
if (!b1) { // b1 is true !true => false // condition fails
x = x + 10;
} else if (b2 = true) { // condition pass
x = x + 100; // x will be 101
System.out.println(x);
} else if (b1 | b2) { // skipped because presvious condition passed
x = x + 1000;
}
}
}
System.out.println(x); // printed value is 101
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.