Comp 110 if, if-else, and else-if

if and if-else

if

Using booleans or boolean expressions, which evaluate to true or false, we can create complex logical programs. If-then statements involve the keyword if, parentheses (), a conditional statement, curly braces {} and some code to execute!

if (<conditional statement>) {      
   // code to execute
}

The conditional statement acts as a gate to the code contained within its curly braces, only allowing the program to access and execute the code in the braces if the expression evaluates to true.

The code outside the if-block is completely independent from the conditional and will execute regardless of the conditional statement's truth value.

else

Adding the else keyword to an if-statement allows the programmer to provide an additional code block in the case that the conditional statement evaluates to false.

if (<conditional statement>) {      
    // code to execute
} else {
    // code that executes when conditional statement is false
}

The code within the else block (surrounded by the curly braces after the else keyword and after the comment) executes only when the conditional is false. Therefore, only one of the code blocks (that in the if-block or that in the else-block) executes.

There are tons of conditional operators that can be used within if-else blocks!

Nesting if statements and else statements

Within the then block of an if statement or within an else block we can write as many statements as we want.

We also have just learned that if-then and if-then-else are themselves statements. This means we can put if-then and if-then-else statements within then blocks or else blocks. That may sound a little confusing, but the key point is that anywhere we have a statement we can use an if or if-then-else statement, so we can nest these! Nesting if statements gives us more control and lets us be more specific with different options for our program. Let's look at an example to clear things up:

import { print } form "introcs";
 
let person = "Stanley";
 
print("What's my favorite food?");
 
if (person === "Kevin") {
  print("chili");
} else {
  if (person === "Stanley") {
    print("pretzels");
  } else {
    print("cake");
  }
}
 
/* this program will print:
   What's my favorite food?
   pretzels
*/

else-if syntax

Since it is common for us to nest if-then and if-then-else statements, we've got a way to make this neater and simpler to use. This is where the else-if comes in. Instead of opening an else block, then inside of that opening another if-block, we can combine these steps. This is how we get an else-if block. It does the same as an if block inside of an else block, but it's shorter to write!

Here are the steps to convert from an if nested inside an else block to one else-if:

  1. Take out the opening curly brace after the else and take out the matching closing curly brace
  2. Move the nested if to be right next to the else keyword, and fix the indentation and spacing

Here's the example from above, but this time using an else-if:

import { print } form "introcs";
 
let person = "Stanley";
 
print("What's my favorite food?");
 
if (person === "Kevin") {
  print("chili");
} else if (person === "Stanley") {
    print("pretzels");
} else {
    print("cake");
}
 
/* this program will print:
   What's my favorite food?
   pretzels
*/

Using if-then, if-then-else, and else-if statements help us direct the logical flow of a program by increasing our control over which boolean combinations lead to which output. We can make more complex code that does more with fewer lines!