The answer is no - the if-statement is not like a switch-statement without a break. Let us begin with this part of your question:
if boolean_expression_1 was true, would it check if boolean_expression_2 is true?
We can find the answer with this program. The trick is, that we call a method as our boolean_expression and use System.out.println() to see, if the method gets executed. This way we know if the expression was evaluated. Here is the program:
public class IfElseExample {
private static boolean booleanExpression1() {
System.out.println("Expression 1 is being evaluated.");
return true;
}
private static boolean booleanExpression2() {
System.out.println("Expression 2 is being evaluated.");
return true;
}
public static void main(String[] args) {
if (booleanExpression1())
System.out.println("Statement 1 is executed");
else if (booleanExpression2())
System.out.println("Statement 2 is executed");
else
System.out.println("Default statement is executed");
}
}
You will see, that the second boolean expression is not evaluated. This behavior probably becomes more understandable if you put in some brackets (which is a good habit in most cases):
if (booleanExpression1()) {
System.out.println("Statement 1 is executed");
} else {
if (booleanExpression2()) {
System.out.println("Statement 2 is executed");
} else {
System.out.println("Default statement is executed");
}
}
This is the way, the compiler views your original if-chain. As you can see now, the second "if" is a single statement of its own and as such it is not on the same level as the first if-statement. If you chain more if-statements those get nested even deeper.
You also asked, why the switch-statement needs a break statement. This is because the switch-statement is not implemented internally with a chain of if-statement (neither is the if-statement based upon switch-statements). You might expect that
switch(controllingExpression()) {
case 42: System.out.println("A");
case 43: System.out.println("B");
default : System.out.println("z");
}
gets translated by the compiler into something like:
if (controllingExpression() == 42) {
System.out.println("A");
} else if (controllingExpression() == 43) {
System.out.println("B");
} else {
System.out.println("z");
}
This is not the case. Instead it uses a hidden skip-statement:
int x = numberOfStatementsToSkipFor(controllingExpression());
skip x // hidden skip the next x statements byte code instruction
System.out.println("A");
System.out.println("B");
System.out.println("z");
The method numberOfStatementsToSkipFor() returns 0 for 42, 1 for 43 and 2 for everything else. Now it becomes clear why this program might produce
A
B
z
or
B
z
or just
z
but never
A
B
without the 'z'. But clearly it would be nice if we could do this. And we can by using break which gets translated into another skip statement. So if you put break into all case-branches
switch(controllingExpression()) {
case 42: {
System.out.println("A");
break;
}
case 43: {
System.out.println("B");
break;
}
default : System.out.println("z");
}
the compiler will produce this:
int x = numberOfStatementsToSkip(controllingExpression());
skip x; // hidden skip the next x statements byte code instruction
System.out.println("A");
skip 3; //"B" + skip + "z"
System.out.println("B");
skip 1;
System.out.println("z");