πŸ““3.3: If-Else Statements

Table of Contents


πŸ“– This page is a condensed version of CSAwesome Topic 3.3

πŸ“ Take notes in a Codespace during class, coding along with the instructor.

  1. Go to GitHub and click on your picture in the TOP RIGHT corner
  2. Select Your repositories
  3. Open CS2-Unit-3-Notes
  4. Now on your repository, click and select the Codespaces tab
  5. Click Create Codespace on main (unless you already have one listed there), wait for the environment to load, then you’re ready to code!

Two-way Selection: if-else Statements

What if you want to pick between two possibilities? If you are trying to decide between a couple of things to do, you might flip a coin and do one thing if it lands as heads and another if it is tails. In programming, you can use the if keyword followed by a statement or block of statements, and then the else keyword also followed by a statement or block of statements.

// A block if/else statement
if (boolean expression) {
  statement1;
  statement2;
}
else {
  statement3;
  statement4;
}

The following flowchart demonstrates that if the condition (the boolean expression) is true, one block of statements is executed, but if the condition is false, a different block of statements inside the else clause is executed:

image

The else block will only execute if the condition in the if-statement block evaluates to false!

Relational Operators in If-Else Statements

If/else statements can also be used with relational operators and numbers like below. If your code has an if/else statement, you should test it with 2 test-cases to make sure that both blocks of the code work.

  1. Test the following code to see what it prints out when the variable age is set to the value 16:
    int age = 16;
    if (age >= 16) {
      System.out.println("You can get a driver's license in most states!");
    }
    else {
      System.out.println( "Sorry, you need to be older to get a driver's license.");
    }
    
  2. Change the variable age’s value to 15 or younger, and then run it again to see the result of the print statement in the else part.
  3. Can you modify the if-statement to indicate that you can get a license at age 17 instead of 16? This is true for the state of New York.

    Again, try 2 test cases for the value of age to test your code to see the results of both print statements.

Nested Ifs and β€œDangling Else”

If statements can be nested inside other if statements. This is like asking a β€œfollow-up question” after asking passing the first one.

Sometimes with nested ifs we find a β€œdangling else” that could potentially belong to either if statement. The rule is that the else clause will always be a part of the closest unmatched if statement in the same block of code, regardless of indentation.

    // Nested if with dangling else
    if (boolean expression)
       if (boolean expression)
          Do statement;
       else  // belongs to closest if
          Do other statement;

πŸ’» In-Class Activity: 20 Questions

Have you ever played 20 Questions? 20 Questions is a game where one person thinks of an object, and the other players ask up to 20 questions to guess what it is.

There is great online version called Akinator that guesses whether you are thinking of a real or fictional character by asking you questions. Akinator is a simple Artificial Intelligence algorithm that uses a decision tree of yes or no questions to pinpoint the answer.

Although Akinator needs a very large decision tree, we can create a guessing game for just animals using a much smaller number of if-statements.

  1. Go to
  2. Make sure you SIGN IN!
  3. Complete the Programming Challenge: 20 Questions activity in pairs.

image


⭐️ Summary

  • If statements can be followed by an associated else block to form a 2-way branch:
    // A block if/else statement
    if (boolean expression) {
    statement1;
    statement2;
    }
    else {
    statement3;
    statement4;
    }
    
  • A two way selection (if/else) is written when there are two sets of statements: one to be executed when the Boolean condition is true, and another set for when the Boolean condition is false.
    • The body of the β€œif” statement is executed when the Boolean condition is true, and the body of the β€œelse” is executed when the Boolean condition is false.
    • Use 2 test-cases to find errors or validate results to try both branches of an if/else statement.
  • The else statement attaches to the closest unmatched if statement in the same block of statements.

πŸ›‘ When class ends, don’t forget to SAVE YOUR WORK!

  1. Navigate to the Source Control menu on the LEFT sidebar
  2. Type a brief commit message in the box, for example: updated Main.java
  3. Click the button on the LEFT menu
  4. Click the button on the LEFT menu
  5. Finally you can close your Codespace!

Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.