100% found this document useful (2 votes)
8K views5 pages

U5l07 Activityguide Willitcrash

The document introduces a game called "Will it Crash?" where the player traces code using if-statements and robot commands to determine if the robot will crash or its ending position. It provides examples of different types of if-statement patterns like basic if, sequential if, if-else, and nested if/else statements. For each example, the player must trace the robot's movements for 3 scenarios to understand how changing conditions can affect the outcome. The goal is to practice using different if-statement structures to control program flow and predict results.

Uploaded by

api-332412793
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
8K views5 pages

U5l07 Activityguide Willitcrash

The document introduces a game called "Will it Crash?" where the player traces code using if-statements and robot commands to determine if the robot will crash or its ending position. It provides examples of different types of if-statement patterns like basic if, sequential if, if-else, and nested if/else statements. For each example, the player must trace the robot's movements for 3 scenarios to understand how changing conditions can affect the outcome. The goal is to practice using different if-statement structures to control program flow and predict results.

Uploaded by

api-332412793
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Unit 5 - Lesson 9

Name(s)_______________________________________________ Period ______ Date _____________

Activity Guide - Will it Crash?

Lets play a game: Will it Crash?


Each row in the table below presents a small program that uses if-statements and robot commands. Trace
the code and plot the movements of the robot for the 3 scenarios shown to the right of the code. If the robot
is directed to move onto a black square, it crashes and the program ends. If the robot doesnt crash, then
draw a triangle showing its ending location and direction.

There are a few patterns to the ways if-statements are typically used:
Basic If-statements
Sequential If-statements
Basic If-else statements
Nested If and if-else statements.
Combinations of all of the above

Each section below presents an example of one of these common patterns, followed by a few problems for
you to try. For each type study, and make sure you understand, the example a nd why each of the 3
scenarios ends up in the state shown.

EXAMPLE: Basic If-statement


Code is executed sequentially from top to Scenario 1: Scenario 2: Scenario 3:
bottom. The code inside the if-block
executes ONLY if the condition is true,
otherwise the block is skipped and
execution picks up on the first line after the
if-block.

MOVE_FORWARD ()
IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
MOVE_FORWARD ()
}
ROTATE_LEFT () Use the diagram to
MOVE_FORWARD () trace each robot move.

YOU TRY IT - Basic If-statement

ROTATE_LEFT ()
IF (CAN_MOVE (left))
{
ROTATE_LEFT ()
}
MOVE_FORWARD ()
MOVE_FORWARD ()

1
EXAMPLE: Sequential If-statements
Lines of code, including if statements, are
evaluated separately, one at a time, in
order from top to bottom. An if-block
executes ONLY if the expression is true.
Note that an earlier if-statement might
change the state of the of world for an
if-statement that comes later. This makes
it hard to predict what will happen unless
you trace the robot moves and take each
line one at a time.

IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
}
IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
}
ROTATE_LEFT ()
IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
}

YOU TRY IT - Sequential If-statements

ROTATE_LEFT ()
IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
}
ROTATE_RIGHT ()
IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
}
ROTATE_LEFT ()
IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
}

IF (CAN_MOVE ( left ))
{
ROTATE_LEFT ()
MOVE_FORWARD ()
}
IF (CAN_MOVE ( left ))
{
ROTATE_LEFT ()
MOVE_FORWARD ()
}
IF (CAN_MOVE ( left ))
{
ROTATE_LEFT ()
MOVE_FORWARD ()
}

2
EXAMPLE: If-else Statement
The code in the if-block executes ONLY if
the expression is true, otherwise the code
in the else block will run. But one or the
other must execute. An else statement
can be attached to a single if-statement.

ROTATE_LEFT ()
IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
}
ELSE{ NOTE: Easy to miss
ROTATE_LEFT () MOVE_FORWARD on
ROTATE_LEFT () the last line
}
MOVE_FORWARD ()

YOU TRY IT - Simple If-Else

Here is a block-based
version of a very similar
program.

IF (CAN_MOVE (left))
{
ROTATE_LEFT ()
MOVE_FORWARD ()
}
ELSE
{
ROTATE_RIGHT()
MOVE_FORWARD ()
}
IF (CAN_MOVE (right))
{
ROTATE_RIGHT ()
}
ELSE
{
ROTATE_LEFT ()
}
MOVE_FORWARD ()

3
EXAMPLE: Nested Statements
You can put if- and if-else statements
inside other if-statements. All previous
rules apply, but tracing the code can be
tricky.

IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
}
ELSE
{
IF(CAN_MOVE( backward ))
{
ROTATE_LEFT ()
ROTATE_LEFT ()
MOVE_FORWARD ()
}
MOVE_FORWARD ()
}
MOVE_FORWARD ()

YOU TRY IT - Nested If-statements

IF (CAN_MOVE (forward))
{
IF (CAN_MOVE (left))
{
ROTATE_LEFT ()
}
ELSE{
ROTATE_RIGHT ()
}
MOVE_FORWARD ()
}
ELSE
{
ROTATE_LEFT ()
ROTATE_LEFT ()
}
MOVE_FORWARD ()

4
Challenge: putting it all together if, if-else, sequential if, nested statements

IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
IF (CAN_MOVE (left))
{
ROTATE_LEFT ()
IF (CAN_MOVE (right))
{
ROTATE_RIGHT()
}
}
}
MOVE_FORWARD ()

IF (CAN_MOVE (forward))
{
MOVE_FORWARD ()
IF( CAN_MOVE (left))
{
IF( CAN_MOVE (right))
{
ROTATE_RIGHT ()
}
ELSE
{
ROTATE_LEFT ()
}
}
ELSE
{
ROTATE_RIGHT ()
}
MOVE_FORWARD ()
}
ELSE
{
ROTATE_LEFT ()
ROTATE_LEFT ()
}
MOVE_FORWARD ()

You might also like