Every day, we make lots of decisions, from small ones like choosing what to wear to bigger ones like deciding what to do after school. These choices often depend on different factors, like the weather or our plans. Just like us, computer programs also need to make decisions based on changing conditions.
In programming, we use something called conditional statements to help computers make these decisions. In a previous lesson, we learned about boolean expressions, which are statements that can be either true or false. Now, let’s see how we can use these expressions to guide a computer’s actions.
The simplest type of conditional statement is the “if” statement. It tells the computer, “If this condition is true, then do this specific task.” For instance, imagine you want to create a program that checks if someone is old enough to watch a PG-13 movie. The question for the computer is, “Is the person’s age 13 or older?”
In this case, the boolean expression “age greater than or equal to 13” is placed next to the “if” statement. If this condition is true, the program will display a message saying the person can watch the movie.
But what if the person is not old enough? We don’t want the program to do nothing. This is where the “else” statement comes in. By adding an “else” statement, we can tell the computer what to do when the “if” condition is false.
So, if the age is less than 13, the program can display a different message saying the person is not old enough to watch the movie. This way, the program can handle both situations: when the condition is true and when it’s false.
In App Lab, a tool for creating programs, you can easily add an “else” statement. You can do this by dragging out the “if/else” block or by clicking the “+” symbol in the bottom right corner of an “if” block. This makes it simple to create programs that can make decisions just like we do!
By understanding and using “if” and “else” statements, you can make your programs smarter and more responsive to different situations. This is a fundamental skill in programming that helps create dynamic and interactive applications.
Create a choose-your-own-adventure story using “if/else” statements. Write a short story where you, the reader, make decisions at key points. Use conditional statements to determine the outcome of each choice. Share your story with classmates and see how different choices lead to different endings.
Write a simple program that asks for a user’s age and uses “if/else” statements to determine if they can watch a PG-13 movie. Test your program with different ages to ensure it displays the correct message for each scenario.
Design a program that suggests what to wear based on the weather. Use “if/else” statements to check conditions like temperature and rain. For example, if it’s raining, suggest wearing a raincoat. Share your program with friends and see if they agree with your outfit choices!
Create a short quiz with questions that have multiple-choice answers. Use “if/else” statements to check if the selected answer is correct and provide feedback. Challenge your classmates to take your quiz and see who scores the highest!
Use App Lab to create a simple app that uses “if/else” statements. Experiment with different conditions and actions. For example, create an app that changes the background color based on user input. Share your app with the class and explain how it works.
**CONDITIONALS PART 2A: IF/ELSE STATEMENTS**
Our daily lives are filled with decisions, both big and small. Even simple choices, like what to wear, usually result from complex considerations. We might think about the weather, our plans for the day, or whether we want to make an impression or keep it casual. The world around us is constantly changing, and we want to adapt our choices to match these changing 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 previous 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 indicates that “if something 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 we want the computer to evaluate is, “Is age greater than or equal to 13?” The boolean expression goes next to the “if,” and the block of code we want to run—only if the condition is true—goes inside the “if” statement.
In our example, whenever the expression “age greater than or equal to 13” is true, the program will display that you are old enough to see a PG-13 movie. Of course, some people will not be old enough, and in these situations, we want our programs to do something else when the condition is false. To handle this, we can expand our “if” statement to include an “else” statement. The commands contained 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 either dragging out the “if/else” block or by clicking the “+” symbol in the bottom right corner of the “if” block.
Conditionals – Statements in programming that only run when a certain condition is true. – Example sentence: In our coding class, we learned how to use conditionals to make a game character jump only when the space bar is pressed.
Statements – Instructions written in a programming language that perform a specific action. – Example sentence: Each line of code in our program is made up of statements that tell the computer what to do.
Boolean – A data type that can only have two values: true or false. – Example sentence: We used a boolean variable to check if the player has completed the level in our game.
Expressions – Combinations of variables, operators, and values that produce a result. – Example sentence: In our math app, we used expressions to calculate the total score based on the player’s answers.
Age – A variable often used to store the number of years since something was created or born, commonly used in programs to determine access or content suitability. – Example sentence: The app checks the user’s age to ensure they are old enough to access certain features.
Program – A set of instructions that a computer follows to perform a task. – Example sentence: We wrote a program in class that can solve simple math problems automatically.
Else – A keyword used in programming to specify an alternative action if a condition is false. – Example sentence: If the user enters the correct password, they can log in; else, they see an error message.
Decisions – Choices made by a program based on conditions and logic. – Example sentence: Our game makes decisions on which path to take based on the player’s input.
App – A software application designed to perform a specific task on a computer or mobile device. – Example sentence: We created an app that helps students learn coding through fun challenges.
Interactive – Allowing a user to engage and respond to the program’s actions. – Example sentence: The interactive tutorial helped us understand how to write code by letting us try each step ourselves.