1. Making Decisions in Programs with Conditional Statements Let's imagine that we want to write a program for an amusement park ride to check a person's height. If they're 48 inches or taller, the program will display a "you're tall enough" message. To make decisions like this in our programs, we use a conditional statement. It's called a "conditional" statement because the decision depends on a condition. It is also sometimes called a selection statement, because it is selecting which parts of the code to execute. In the example above, the condition is whether the user's height is greater than 48 inches or not. Conditional statements are also called if statements, because they use the word "if": if the height is greater than or equal to 48, then display the message. But otherwise don't display it.
2. The if Block In RoboBlocky we use the if block for a basic conditional statement, found in the Logic menu:
We combine it with the relational operators block (also in the Logic menu):
The next section shows how to put it all together.
3. Example: Checking the Height Here's how we would write the program to check a person's height. It prompts the user to enter their height and then displays a message if they're 48 inches or taller:
Here's what the code does, in order:
Example 2 uses a conditional statement to move the robot if a random number comes up even.
|
conditional statement, selection statement, if statement, if block
|
The code prompts the user to enter their height. If the height is greater than or equal to 48, it displays a message, and then displays "All done." Otherwise it skips the message and just displays "All done." Sample output if the user enters 48:
|
Store a random number between 1 and 100 in a variable. Print the value, and then if it's an even number (divisible by 2), drive the robot 12 inches. |
Grid Size: S M L | Simple View: | MinView: |
Coord: x-axis y-axis | Grid Lines: x-axis y-axis | |||
Grid: | 12x12 inches | 24x24 inches | 36x36 inches | |
72x72 inches | 96x96 inches | 192x192 inches | ||
1x1 inches | 2x2 inches | Fraction: | ||
xy-Range: | ||||
|
||||
|
||||
Quad: | 1 Quadrant | 4 Quadrants | 1&4 Quadrants | |
Units: | US Customary | Metric | Ruler: |
Labels: |
|
|
|
Font
px
|
|
Tics Lines: |
|
Width
px
|
Hash Lines: |
|
Width
px
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
|
|||||||||||
|
|||||||||||