If / Else / Else If Conditionals
Using conditionals is one way to earn more points in the "Coding Complexity" line of the judging rubric. It can also help with "App Function" and "User Experience and Design".
In this lesson, you will…
- Learn how to write an If / Else / Else If Conditional Statement
- Improve your Magic 8 ball app by not allowing the user to ask the same question twice in a row
If / Else / Else If
So far you’ve learned how to write conditional statements that check conditions to decide if they are true or false. The conditional statements you created in the last coding lesson had two possible outcomes. In this lesson, you’ll learn how to write conditional statements that check more than one condition and have more than two possible outcomes.
If / Else / Else If conditional statements are conditional statements that have more than one condition. If the first condition is false, only then will the second condition will be checked. If the second one is also false, then the app will default to else or it will do nothing. Check out the diagram below. The conditions are blue and the possible outcomes are in purple.
A conditional statement with one condition and two possible outcomes.
A conditional statement with two conditions and three possible outcomes. Condition 2 is only checked if Condition 1 is false.
Here’s what the blocks look like in App Inventor:
Your first condition goes next to if, and your second condition goes next to else if. Let’s look at an example:
You are designing an app that is only meant for users ages 13-18 and you want your app to let your users know if they are too young or too old to use your app. You have three possible outcomes:
- The user is too young
- The user is too old
- The user is the right age
You have the user enter their age into the app and store it in a variable called “age”. Here’s how you could use an if/else if/else statement to check your user's age.
The first condition checks if the user is younger than 13. If she is, then the app alerts her that she is too young. If she is not, the app then checks if she is older than 18. If she is, then the app alerts her that she is too old. If she is not, then the app tells her she is the right age.
You can add as many conditions as you want to your conditional statement by using else if, but it’s really important to pay attention to the order in which the app checks the conditions. Your code will always start with the first condition and then continue in order until it finds a condition that is true. When it finds a condition that is true it will carry out the code under it. If none of the conditions are true, it will carry out the code in the else part.
Here are some things to remember when using if/else/else if statements:
- You can test as many conditions as you want
- The statement works from top to bottom of the block, so put the condition you want to be tested first at the top of the statement
- The only code that will be carried out is the code under the first statement found to be true
The next coding lesson will cover logic and some more advanced conditionals that senior division students should try. If your team is in the junior division and this is your first time learning how to code, you may skip it.
Activity: Even Better Magic 8 Ball
In this activity, you’re going to improve your magic 8 ball app. In the last lesson, you made sure that the user put in some text before getting an answer. However, the user didn’t need to ask a new question to get a new answer. They could keep pressing enter, and as long as there was text in the text box, our app would give them an answer.
Let’s fix this. We are going to use an if / else / else if conditional statement to check if the user has entered any text, and then if they have entered text, we will check to see if this question this a different question than the last question asked.
We set up a variable called lastQuestion, which is going to remember the last question that the user asked.
Here’s how our code works. Each time the user gets an answer from our magic 8 ball, our code updates the variable lastQuestion with whatever is in the textbox.
Now you need to figure out how to check that the variable lastQuestion is not the same as what the user has in the textbox now. You’ll need to finish this If / Else / Else if statement. Download the example code to get started.
In this lesson, you learned how to use If Else and Else If conditional statement.
- What are some other conditions that might be helpful to add to the magic 8 ball app?
- Can you think of some places in your app that you might want to use it?