3. Using Conditional Statements and Loops
Using conditional statements (if/else) and loops (while/for), which are core elements of programming, you can make the RUR robot perform more complex and intelligent decisions and actions.
3.1. Conditional Statements: if and else
Use conditional statements to change the robot's behavior based on specific conditions.
# conditional_move.py
if front_is_clear(): # If there's no wall in front of the robot
move() # Move forward
else: # Otherwise (if there is a wall)
turn_left() # Turn left
RUR-PLE provides built-in functions like front_is_clear(), left_is_clear(), right_is_clear(), on_beeper(), and carries_beepers() that can be used as conditions.
3.2. Loops: while and for
Use loops to repeat a specific action or to repeat actions until a condition is met.
while Loop (Repeats as long as the condition is true)
# clear_path.py
while front_is_clear(): # Keep moving as long as there's no wall in front
move()
This code makes the robot move forward until it hits a wall.
for Loop (Repeats a specified number of times)
# four_moves.py
for i in range(4):
move()
turn_left()
This code makes the robot move along a square path (moving and turning 4 times).