Minds On
Conditions and action
Consider the following conditions related to the weather:
- raining outside
- sunny outside
- snow on the ground
For each condition, select the corresponding action.
Action
Part 1
In Minds On, we examined weather conditions and what types of decisions we might make based on the weather condition.
With coding, computers use something called conditional statements to make decisions based on certain conditions.
A conditional statement is phrased as:
if <condition is true> then
<do this>
The condition is checked by the computer. If it is true, then the computer will perform an action.
For example, for our first weather example in the Minds On section, our conditional statement would be:
if raining outside then
bring an umbrella
Consider
-
What is the condition in the conditional statement:
if raining outside then
bring an umbrella
- What is the action that occurs if the condition is true?
- What would the conditional statements be for the other examples from the Minds On table? Remember to use the words “if” and “then.”
For each condition, select the correct corresponding action.
Part 2
Let’s consider pseudocode for conditional statements. These will include examples that we might code a computer to do.
Pseudocode is an informal way of describing a computer program or algorithm that is an intermediary between everyday language and programming code.
Example 1
Here’s some code we might come across in a computer game where the user gets a point for guessing the correct number:
if userGuess == secretNumber then
output: change userPoints by 1
In this example, our program is checking the condition to confirm if the userGuess entered is equal to the secretNumber the computer has stored in its memory.
In this case, we used something called a comparison operator. In many coding languages, the symbol we use for a comparison operator to check if something is equal to something else, is two equal signs (==). If the condition is true, and the user guessed the correct number, they will get a point.
Consider another example of a conditional statement using a different comparison operator. The user is asked to enter an angle between 0 and 180:
if angle > 90 then
output: “obtuse”
The computer is checking to confirm if the angle is greater than 90, and if it is, the computer would say “obtuse.” In this example, we’ve used a greater than comparison operator.
Try it
- Write a conditional statement to check if the angle is less than 90 degrees, and if it is true, then output “acute.”
- Write a conditional statement for to check if the angle is a right angle.
Click ‘Reveal’ to check your answers.
-
if angle < 90 then
output: “acute”
-
if angle == 90 then
output: “right angle”
For each condition, select the correct answer from the drop-down menu.
Consolidation
Writing conditional statements
Now it’s your turn to write conditional statements.
Try to come up with three or more conditional statements.
Write the conditional statements using pseudocode.
Challenge: use a conditional operator in your conditional statements.
Reflection
As you read through these descriptions, which sentence best describes how you are feeling about your understanding of this learning activity? Press the button that is beside this sentence.
I feel...
Now, record your ideas using a voice recorder, speech-to-text, or writing tool.