BEI - if Else Statements (Lesson)
if Else Statements
Introduction
Using if statements we can decide to execute some lines of code or skip them. Now, let’s consider if we have multiple options. Think of the grade example from our previous lesson. Instead of printing “Pass” or “Fail”, let’s print a letter grade. Consider the following grading scale:
A: = 90
B: 90 and = 80
C: 80 and = 75
How would you write code to print out these three different letter grades?
if...else
In our grade example, we can print a statement for getting an A and then another statement for all grades that are not an A.
The else comes after the body of the if. The keyword else is placed after the if braces and the else body is also enclosed in braces. As we saw previously, when the if boolean condition is true, it will execute the if body code. When the boolean condition is true, it will skip the else block of code. When the boolean condition is false, the if body of code will be skipped, and the else block will be executed.
We need to keep going though because our grade example has more than one output. Our grade example has several possible outputs. To incorporate all the different possibilities, we can use nested if…else statements.
Notice that there is an else to go with each if. If the if portion is not executed the else portion will be executed.
We can take that same code and make it even easier to read. Since each if and else body only contains one line of code, we can leave out the braces. When braces are not used it means that only the next statement will be part of the if or else body.
This code will execute the same way as the previous example:
One Final Modification
Now take the same code and write it using else-if statements. Since we have multiple conditions to check we can write else-if statements which is like having multiple else’s only there are conditions to execute them:
Things to remember:
- There is no semi-colon at the end of the if statement.
- if is a lowercase keyword.
- When the body of the if/else statement only contains one line of code, braces are not necessary
- When there are no braces following an if/else statement, Java assumes the next proceeding line is the only line within the body.
- With if/else and if/else if/else structures, exactly one of the statements will execute.
Click on "Runestone Academy" below to open the required reading that is listed.
Runestone Academy: AP CSA – Java Review Links to an external site.:
READ: 5.1 – Conditionals
5.2 – Three or More Options
Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.
Introduction to Computer Science Using Java Links to an external site.:
READ: Chapter 17: The Single Branch if Statement
Complete the quiz for practice.
if else Review
Click below to begin the if else review activity.
Practice-It! Self-Check
- Go to the PracticeIt website Links to an external site..
- Log into account.
- Click on Start Practicing!
- Go to the most recent edition.
- Click on Chapter 4: Conditional Execution.
- Complete Self-Checks: 4.5 – 4.6, 4.10 - 4.12
IMAGES CREATED BY GAVS