Code | Robot Scenario | Commentary | ||
1 | ROTATE_LEFT () |
| Before: The starting scenario before any lines have been executed. Before reading the rest of the page, you might want to try to predict where the robot will end up. | |
2 | IF (CAN_MOVE (forward)) | |||
3 | { | |||
4 | MOVE_FORWARD () | |||
5 | } | |||
6 | ROTATE_LEFT () | |||
7 | IF (CAN_MOVE (forward)) | |||
8 | { | |||
9 | MOVE_FORWARD () | |||
10 | } | |||
1 > | ROTATE_LEFT () |
| Line 1 executes: Robot turns 90 degrees to the left | |
2 | IF (CAN_MOVE (forward)) | |||
3 | { | |||
4 | MOVE_FORWARD () | |||
5 | } | |||
6 | ROTATE_LEFT () | |||
7 | IF (CAN_MOVE (forward)) | |||
8 | { | |||
9 | MOVE_FORWARD () | |||
10 | } | |||
1 | ROTATE_LEFT () |
| Line 2 executes: | |
2 > | IF (CAN_MOVE (forward)) | |||
3 | { | |||
4 | MOVE_FORWARD () | |||
5 | } | |||
6 | ROTATE_LEFT () | |||
7 | IF (CAN_MOVE (forward)) | |||
8 | { | |||
9 | MOVE_FORWARD () | |||
10 | } | |||
1 | ROTATE_LEFT () |
| Line 4 executes: Since the condition was TRUE, execute the line(s) of code inside the if-statement - in this case: move_forward. NOTE: Lines 3 and 5: The “curly braces” { } encapsulate the lines of code to execute if the condition is true. | |
2 | IF (CAN_MOVE (forward)) | |||
3 | { | |||
4 > | MOVE_FORWARD () | |||
5 | } | |||
6 | ROTATE_LEFT () | |||
7 | IF (CAN_MOVE (forward)) | |||
8 | { | |||
9 | MOVE_FORWARD () | |||
10 | } | |||
1 | ROTATE_LEFT () | Line 6 executes: Rotate the robot 90 degrees to the left | ||
2 | IF (CAN_MOVE (forward)) | |||
3 | { | |||
4 | MOVE_FORWARD () | |||
5 | } | |||
6 > | ROTATE_LEFT () | |||
7 | IF (CAN_MOVE (forward)) | |||
8 | { | |||
9 | MOVE_FORWARD () | |||
10 | } | |||
1 | ROTATE_LEFT () | Line 7: Check to see if the robot can move forward now... FALSE - it cannot move forward. The condition fails, so IGNORE the block of code in the curly braces. The robot actually does nothing here. NOTE: the condition of an if-statement is always executed - the blocks inside it though, only run if the condition is TRUE. | ||
2 | IF (CAN_MOVE (forward)) | |||
3 | { | |||
4 | MOVE_FORWARD () | |||
5 | } | |||
6 | ROTATE_LEFT () | |||
7 > | IF (CAN_MOVE (forward)) | |||
8 | { | |||
9 | MOVE_FORWARD () | |||
10 | } | |||
1 | ROTATE_LEFT () | End state: Since there are no more lines of code to execute, the program ends in this state. All lines have been processed. If there were any code starting on line 11, we would attempt to execute that next. | ||
2 | IF (CAN_MOVE (forward)) | |||
3 | { | |||
4 | MOVE_FORWARD () | |||
5 | } | |||
6 | ROTATE_LEFT () | |||
7 | IF (CAN_MOVE (forward)) | |||
8 | { | |||
9 | MOVE_FORWARD () | |||
10 | } | |||
You try it - Same Code, Different Scenario
Because the code that runs depends on the conditions at the time of execution, the same program might end up with different results given a different starting scenario. Try tracing through the same program again, but with a different initial robot setup. What happens? Where does the robot end up?
1 | ROTATE_LEFT () | |||
2 | IF (CAN_MOVE (forward)) | |||
3 | { | |||
4 | MOVE_FORWARD () | |||
5 | } | |||
6 | ROTATE_LEFT () | |||
7 | IF (CAN_MOVE (forward)) | |||
8 | { | |||
9 | MOVE_FORWARD () | |||
10 | } | |||