03Decisions

# 03Decisions - sequence of commands under the “then”...

This preview shows pages 1–3. Sign up to view the full content.

7/11/2007 Decisions Comp106 7/11/2007 Decisions • What if the robot needs to choose between alternative actions? • We can use a Yes/No block in RCX to allow decisions to be made. • A Yes/No block is a type of conditional command.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
7/11/2007 Conditionals – if/then • A Yes/No block, when expanded, shows you a conditional structure. • Actions are performed based on whether or not a condition is true or false. For example: If (touch sensor #1 is pressed) Forward 1.0 second EndIf 7/11/2007 Conditionals – if/then/else • A conditional structure also allows the robot to choose between alternatives: If (touch sensor #1 is pressed) Forward 1.0 second Else Play note F for a ¼ beat EndIf
7/11/2007 Combinations The following is a conditional structure with a
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: sequence of commands under the “then” branch: 1. If (touch sensor #1 is pressed) 1.1. Forward 1.0 second 1.2. Turn left 1.0 second 1.3. Dance 1 time 2. Else 2.1 Play note F for a ¼ beat 3. EndIf • Can we combine loops with conditionals? • Proper indentation is vital in order to understand the structure of the code. 7/11/2007 Nested Ifs • We have seen how loops can be nested, i.e. one loop inside another. Similarly, we can have nested Yes/No blocks. • What does the roverbot do when the code in this diagram is executed? • Can you write the pseudo-code for this? • Complex programs can be written by just combining three algorithmic constructs: sequence, loops and conditionals....
View Full Document

{[ snackBarMessage ]}

### Page1 / 3

03Decisions - sequence of commands under the “then”...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online