Every day, we make lots of decisions, whether they’re big or small. Even simple choices, like deciding what to wear, can be influenced by many factors. You might think about the weather, what activities you have planned, or whether you want to dress up or keep it casual. Since the world around us is always changing, we adjust our choices to fit these conditions.
Just like us, computer programs also need to make decisions. We don’t want them to do the same thing every time; instead, they should react to different situations. In a previous lesson, we learned about boolean expressions, which help us figure out if something is true or false. Now, we can use something called a conditional statement to help computers make decisions based on that information.
The simplest type of conditional statement is an “if” statement. It tells the computer to do something only if a certain condition is true. For example, if you want to create a program that checks if you’re old enough to watch a PG-13 movie, you would use an “if” statement. The condition to check is whether your age is 13 or older. If this is true, the program will say you can watch the movie.
But what if you’re not old enough? In that case, we want the program to do something different. This is where the “else” statement comes in. The “else” part runs only if the “if” condition is false. In App Lab, you can add an “else” statement by using the “if-else” block or by clicking the plus symbol in the “if” block.
Sometimes, there are more than two possible outcomes. Let’s say we want to check if you can watch an R-rated movie, a PG-13 movie, or just a G-rated movie. We can use an “else if” statement to handle these different cases. An “else if” checks another condition only if the previous “if” statements are false.
For example, if your age is 17 or older, the program will say you can watch an R-rated movie. If you’re 13 or older, it will say you can watch a PG-13 movie. If neither of these is true, it will say you can watch a G-rated movie. The “else” part acts as a backup, running when all other conditions are false.
Be careful when using multiple “else if” statements. If you mix up the order, the program might not work as expected. For instance, if you check for PG-13 movies before R-rated ones, everyone 13 or older will be told they can only watch PG-13 movies, even adults. This happens because the computer checks conditions from top to bottom, so it’s important to start with the most specific condition.
Create a decision tree based on your daily routine. Think about the decisions you make from waking up to going to bed. Use “if,” “else if,” and “else” statements to map out your choices. For example, if it’s raining, you might choose to wear a raincoat. Share your decision tree with the class and explain how it helps you make decisions.
Use a simple coding platform like Scratch or App Lab to create a program that makes decisions based on user input. For example, create a quiz that asks questions and gives feedback based on the answers. Use “if,” “else if,” and “else” statements to handle different responses. Share your program with a classmate and try each other’s quizzes.
In groups, come up with real-life scenarios where decisions are made using conditions. Act out these scenarios in front of the class. For example, role-play a situation where you decide what to do if you miss the bus. Discuss how using “if,” “else if,” and “else” statements helps in making decisions.
Write a short story where the main character faces multiple decisions. Use “if,” “else if,” and “else” statements to show different paths the story could take based on the character’s choices. Share your story with the class and discuss how different conditions lead to different outcomes.
Design an interactive flowchart using online tools like Lucidchart or Google Drawings. Choose a topic, such as choosing a pet, and create a flowchart that uses “if,” “else if,” and “else” statements to guide users through the decision-making process. Present your flowchart to the class and explain how it helps in making informed decisions.
Our daily lives are filled with decisions, big and small. Even simple choices, like what to wear, usually result from complex considerations. Factors such as the weather, planned activities, and whether you want to make an impression or keep it casual all play a role. The world around us is constantly changing, so we adapt our choices to match these conditions.
This concept applies to computer programs as well. We don’t want our programs to run in the same way every time; they should respond to changing conditions. In the last video, we learned how to use boolean expressions to determine whether something is true or false. Now, we can add something called a conditional statement to help guide the computer’s decisions based on that information.
The simplest conditional statement is an “if” statement. It specifies that if a certain condition is true, the computer should execute a specific block of code. For example, if you want to create a program that displays whether or not you are old enough to see a PG-13 movie, the question to evaluate is whether your age is greater than or equal to 13. The boolean expression goes next to the “if,” and the block of code to run if the condition is true goes inside the “if” statement. In this case, if the expression is true, the program will display that you are old enough to see a PG-13 movie.
However, some people may not be old enough. In such situations, we want our programs to perform an alternative action when the condition is false. To achieve this, we can expand our “if” statements to include an “else” statement. The commands in the “else” statement will only run if the boolean condition in the “if” statement is false. In App Lab, you can add an “else” statement by dragging out the “if-else” block or by clicking the plus symbol in the bottom right corner of the “if” block.
Sometimes, the decisions your program needs to make have more than two possible outcomes. Continuing with our movie rating example, we want to check whether the highest rating you can see alone is an R-rated movie, a PG-13 movie, or just a G-rated movie. To check between these three cases, we can add an “else if” statement. An “else if” statement checks another condition only if the previous “if” statements were false.
For instance, if your age is greater than or equal to 17, the program will indicate that you can see an R-rated movie alone. If the age is greater than or equal to 13, it will indicate that you can see a PG-13 movie alone. Otherwise, it will state that you can see a G-rated movie alone. The “else” condition serves as the default, executing when all previous “if” and “else if” statements are false.
Be cautious when constructing longer sequences of “else if” statements. If you mix up the order, you might not achieve the intended behavior. For example, if we mistakenly check if the user is old enough to see PG-13 movies first, everyone who is 13 or older would be told they can only see PG-13 movies, including adults. This would lead to incorrect results, as the computer checks conditions from top to bottom. Therefore, it’s crucial to enter the most specific condition first.
Decisions – Choices made by a computer program based on certain conditions. – In coding, decisions help the program know what to do next based on the user’s input.
Computer – An electronic device that processes data and performs tasks according to a set of instructions. – We use a computer to write code and create fun games.
Programs – Sets of instructions that tell a computer what to do. – We wrote programs in class to make the robot move and dance.
Conditional – A feature in programming that performs different actions based on whether a condition is true or false. – We used a conditional to check if the player has enough points to win the game.
Statement – A single line of code that performs a specific task. – Each statement in our program tells the computer to do something, like print a message or add numbers.
If – A keyword used in programming to start a conditional statement that runs code if a condition is true. – We used an if statement to check if the user entered the correct password.
Else – A keyword used in programming to specify what to do if the condition in an if statement is false. – If the user doesn’t enter the correct password, the else part of the code will show an error message.
Boolean – A data type that can only have two values: true or false. – We used a boolean variable to keep track of whether the game is over or not.
Age – A variable that can store a number representing how old someone or something is. – In our program, we asked the user to enter their age to check if they can create an account.
Outcomes – The possible results that can happen based on decisions made in a program. – The outcomes of our quiz program depend on how many questions the user answers correctly.