π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.
- Go to GitHub and click on your picture in the TOP RIGHT corner
- Select
Your repositories
- Open
CS2-Unit-3-Notes
- Now on your repository, click and select the
Codespaces
tab - 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:
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.
- 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."); }
- 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. - 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.
βοΈ 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 isfalse
.- 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 isfalse
. - Use 2 test-cases to find errors or validate results to try both branches of an if/else statement.
- The body of the βifβ statement is executed when the Boolean condition is
- 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!
- Navigate to the
Source Control
menu on the LEFT sidebar - Type a brief commit message in the box, for example:
updated Main.java
- Click the button on the LEFT menu
- Click the button on the LEFT menu
- Finally you can close your Codespace!
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.