The if Statements

if-else Statement

🙋 Need help? Ask an expert now!

The if-else statement is a more complex but useful conditional statement. It executes one branch if the condition is true, and another if it is false. It works similarly to the if statement, where the condition is evaluated before the statements are executed. If the condition results in true, the first batch of statements are executed. If the condition results in false, the second batch of statements are executed. Take a look at the example below and understand the logic behind the if-else statement

As shown in the code above, instead of having two if statements like we previously had, we have an if-else statement that does the exact same thing. If the expression/condition numberTwo > numberOne evaluates to true, the statement biggerNumber = numberTwo is executed. Everything inside the else will be ignored. If the expression evaluates to false, the statement biggerNumber = numberOne will be executed, and the statements inside the if will be ignored. Therefore, you will never execute both statements, only one or the other.

Edit Me on GitHub!

Application Question

Consider the following code segment:

if (10 > 20 && 20 > 10){
            } else {
            if (10 < 20 && 50 % 5 == 10){
            if (10 == 20 || 50 / 0 == 0){

What will the following code print to the console?