Coding 7: If / Else Conditional Statements

If/Else Conditional Statements

Coding 7

Using conditionals is one way to earn more points in the Coding Complexity line of the rubric. It can also help with App Function and User Experience and Design.

In this lesson, you will…

  • Learn about conditional statements and how to write them
  • Improve your magic 8 ball app by making sure the user puts a question in the textbox

Key Terms

  • Conditions -  A state or situation something is experiencing
  • Conditionals Statements -  A way for computers to make decisions based on conditions
  • If/Else - A common form of conditional statements in programming; tells the computer that if the condition is true, do this. Else, if the condition is false, do another thing.

Conditions

So far, you have been using a lot of event handlers. In the past few activities, when your user pushes a button, your app did something. For example, in the magic 8 ball app from Coding 6 Lists, the enter button added items to a to-do list. The event was the user pushing “enter” and the way the app handled it was by adding the user’s text to the to-do list. The code for your app worked like this:

If you didn’t get a chance to do this activity, you can download the code and try it now.

You may noticed that you just can press enter without asking a question (leaving the box blank), and the app will still give you an answer. Let’s fix this. We don’t want our users to get answers without asking any questions. So, every time the user hits enter, we want our code to check, “has the user entered any text”? If the answer is yes, then the app gives them an answer. If the answer is no, then the app tells them to ask a question. Now your code looks more like this:

Here, "do this" means "give an answer", and "do that" means "tell them to ask a question". This a great way to fix your problem, but remember that computers can’t think for themselves, so you can’t directly ask them questions like you would ask a person.

In coding, when you want to ask your app a question, you program it to check a condition. A condition is something that a computer can decide is either true or false.  True is like the computer is answering yes and false is like answering no. You can tell your app to do different things depending on if the condition is true or false. Here’s how you would ask your app if the user has entered text using a condition:

 

Do you remember the data type booleans from Coding 4: Data and Functions? Booleans are a data type that can be true or false. When your app checks a condition, it gives the answer in the form of a boolean.

Conditional Statements

So now, you know what a condition is, but how do you use it in coding? In coding, you ask your computer to check conditions by writing conditional statements.  Conditional statements are the way computers can make decisions. Conditional statements always have an if part, which tells the app what to do when the condition is true. Conditional statements also usually have an else part, which tells the app what to do when the condition is false. If you leave out the else part then your app will do nothing when the condition is false. Now your code for your enter button would look something like this:

 

Here is what conditional statements look like in App Inventor: 

Here’s how these blocks work. You put a condition next to if. You put the code for what your app should do if the condition is true next to then and what you want your app to do if the condition is false next to else. If the condition is true, only the code next to then will run and all the code next to else will be ignored. If your condition is false, the code next to then will be ignored and the code next to if will run. Think of it as saying "If this condition is true then do this, or else do that".

Let’s go back to the example we were using from the Magic 8 Ball. Here is what the conditional looks like filled in.

Here, when the user clicks the ‘enter’ button the app will check this condition: “the string in the textbox does not equal a blank string”. If this condition is true (the string does not equal a blank string) it means the user has entered text, so the app will give the user an answer. If the condition is false (the string is equal to a blank string), then all the code next to then is ignored, and skips to else, which tells the user to "Ask me a question first!".

There are a few different ways to write this conditional statement. All of them work, there is no correct answer. There are four more examples below. See if you can understand them.

Example 1:

Condition: the length of the string in the textbox is not equal to zero

Example 2:

Condition: the textbox is not empty

Example 3:

Condition: the length of the string in the textbox is equal to zero

(notice that the blocks next to then and else are switched compared to example 1)

Example 4:

Condition: the textbox is empty

(notice that the blocks next to then and else are switched compared to example 1)

Activity: Smarter Magic 8 Ball

It’s time to make your magic 8 ball app better! You want your app to check to see if the user has really entered when they press “enter”. See if you can remember how to create your conditional statement.

Activity - Winner Counting App

This activity builds on the activity from Coding 5: Variables. If you haven’t done that activity yet, try it now!

Now that you’ve mastered conditionals for your magic 8 ball, let improve another one of our old apps, the counting app. Let’s turn this app into game. Try to start the count over as soon as the user reaches 100. So, when the user presses a button to increase the count, you’ll want to check and make sure that they are not going over 100.  Where do you think you should put your conditional statement?

Reflection

In this lesson, you learned about conditional statements. Can you think of some conditional statements that you use to make decisions every day? Here are some examples:

  • If it is raining, then take an umbrella with you, else get wet
  • If it is cold outside, then bring a jacket, else you will freeze!
  • If your phone is dead, then charge it, else you won’t be able to use your phone.
  • If you are sick, then go to the doctor, else go to school.
  • If your clothes are dirty, then wash them, else have no clean clothes to wear!
  • If you eat your vegetables, then you will get dessert, else mom won't let you!