[Robot]
  Reeborg the Robot  

Conditionally Executing Instructions



Reeborg's Sensory Equipment

As you know, Reeborg has three video cameras, a microphone, a compass, and tactile sensors to help him stay out of trouble. He can request the following information from his sensory equipment: Each request will result in a True or False answer. We call such requests that are either True or False predicates.

Conditional Execution

Reeborg can check the predicates above using what is called an IF construct. These constructs can be used wherever instructions are used in a program. In the Python language, there are two variations of the IF statement, the simple IF and the compound IF-THEN-ELSE.

Instruction Syntax
************************************
Explanation
if  predicate:
        <then-block statement(s)>
simple IF

if the predicate evaluates to True:
Reeborg will execute the instruction(s) in the then-block and thereafter the instruction immediately following the then-block.
if the predicate evaluates to False:
Reeborg will not execute the instruction(s) in the then-block but instead will jump to the instruction immediately following the then-block.
if  predicate:          
        <then-block statement(s)>
else:
        <else-block statement(s)>
compound IF-THEN-ELSE

if the predicate evaluates to True:
Reeborg will execute the instruction(s) in the then-block and thereafter the instruction immediately following the else-block. In other words, the then-block gets executed whereas the else-block does not and then the instruction following the if statement is executed.
if the predicate evaluates to False:
Reeborg will execute the instruction(s) in the else-block and then the instruction immediately following the else-block. In other words, the then-block is skipped and the else-block is executed; execution continues thereafter with the instruction following the if statement.

Note that the statements in then-blocks and else-blocks must be indented and indented to the same level. Recall that to indent instructions in a Python program, space characters (i.e., blanks) should be used, not tabs. In Python, 4 blanks are usually used to indent statements.

Here are some examples of IF statements. Note that nested IF's (that is, one IF statement inside another) can sometimes be confusing and it is important to make sure that those types of instructions really work as intended.

Example Explanation
if on_beeper():
    pick_beeper()
move()
Reeborg checks if a beeper is present on the current corner. If there is one, he will pick it up and move one step forward. If there is no beeper present, he will just move one step forward.
if front_is_clear():
    move()
else:
    turn_left()
Reeborg will move ahead one street corner only if his front is not blocked by a wall. If his front is blocked, he will turn left instead.
move()
if condition:
    turn_left()
    turn_left()
move()
Sample if logic This code snippet illustrates a simple IF with two items comprising the then-block. Reeborg will move forward one street corener. Next, Reeborg will twice turn left if and only if condition (some predicate) evaluates to True. In any event, Reeborg will eventually make a second move forward.
def face_north_if_facing_south():
    if facing_south():
        turn_left()
        turn_left()
If Reeborg is facing South, this new command will turn him North. If Reeborg is not facing South, this new command will do nothing at all.
move()
if condition:
    turn_right()
else:
    turn_left()
move()
Compound if-else logic This code snippet directs Reeborg to take a step forward. Next comes a compound IF where Reeborg will either turn right if condition evaluates to True or will turn left, because the condition evaluated to False. In any event, Reeborg will afterwords take a second step forward.
def escape():
    if front_is_clear():
        move()
        turn_left()
    else:
        turn_left()
        move()
Reeborg would move and turn left if his front is clear. Otherwise, he would first turn left and then move. Note that if his front and left side were blocked, this new command would lead to an error shutoff.
def give_or_take():
    if on_beeper():
        if carrying_beepers():
            put_beeper()
        else:
            pick_beeper()
Reeborg would first check if he is next to a beeper. If so, he would add another beeper to the current corner, but only if there are any in his pocket. If he was next to a beeper but without beepers in his pocket, he would pick up the beeper from the current corner. If he was not near a beeper, he would do nothing.
def face_north():
    if not facing_north():
        turn_left()
    if not facing_north():
        turn_left()
    if not facing_north():
        turn_left()
Reeborg would check if he is facing a direction other than North. If so, he'll turn left. Reeborg will make the check twice more, each time turning left only if not already facing North.

Thus if Reeborg was initially facing North, he will do nothing. If Reeborg was initially facing East, he will make only one left turn. If Reeborg was initially facing South, he will make two left turns. If Reeborg was initially facing West, he will make a total of three left turns. At the end of executing this function, Reeborg is always facing North.


Multiple Alternatives

The IF-THEN-ELSE provides an obvious way of selecting which of two alternatives are to be performed in a program. If there are more than two alternatives, nested IF-ELIF-ELSE's can be used. The elif is just Python's shorthand way of starting a nested IF. Usually we indent these nested IF-ELIF-ELSE's in such a way as to highlight that one of a set of mutually exclusive alternatives is to be executed. An example will make things clearer:

Example: Explanation:

    ...
    turn_right()

    if facing_north():   
        turn_left()
    elif facing_east():
        move()
        turn_left()
    elif facing_south():
        pick_beeper()
    else:
        put_beeper()

    pick_beeper_if_present()  
    ...
In the code fragment to the left, we see that between the turn_right() instruction and the pick_beeper_if_present() instruction one of four alternatives will be executed. Which of the mutually exclusive alternatives is done depends on the direction Reeborg is facing when he enters this section of code. If Reeborg is facing to the North, he will turn left before executing the pick_beeper_if_present() instruction. If Reeborg is facing to the East, he will move one block further to the East and then turn left; thereafter he will execute the pick_beeper_if_present() instruction. Similarly, if Reeborg is facing to the South, the alternate action to be performed is picking a beeper; if Reeborg is facing any other direction (of course, the only direction left must be West), Reeborg will put down a beeper.
elif logic Depicted to the left is a 3-way decision. One of three, mutually exclusive, alternatives is to be performed. If we pretend that the predicates it_rains and it_snows existed in Reeborg's world (they don't!), the logic shown could be implemented in Python as follows:
    #...some preceding stuff...

    if it_rains():
        play_indoors()
    elif it_snows():
        go_skiing()
    else:
        go_swimming() # assuming it is warm!

    #...whatever follows...
    
Notice the use of elif (which means else if) for choice 2.



Prior Topic       |       Next Topic  

[MTSU]  | CS  | [Home] Return to Reeborg home page
(Credits and Copyrights)