Lectures - Cip3 - Lecture Slides Control Flow Karel - File
Lectures - Cip3 - Lecture Slides Control Flow Karel - File
“Avenues” run
North/South
2 + + + + + West East
1 + + + + +
South
1 2 3 4 5
• Grid, where “corner” is intersection of each street/avenue
• Karel is currently on corner (1, 1)
• If Karel moved forward, Karel would be on corner (2, 1)
• Karel’s beeper bag can have 0, 1, or more (up to infinite) beepers
if/else
if
while lo w
l F
for Loops t ro
Co n
Loops o f
e r
R iv
The
Piech and Sahami, CS106A, Stanford University
for loop
for i in range(count):
statements # note indenting
def turn_right():
for i in range(3):
turn_left() # note indenting
Place Beeper Square
def main():
for i in range(4):
put_beeper()
move()
turn_left()
def main():
for i in range(4):
put_beeper()
move()
turn_left()
def main():
for i in range(4):
put_beeper()
move()
turn_left()
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti m e
First
g h the
throu p
loo
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti m e
First
g h the
throu p
loo
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti m e
First
g h the
throu p
loo
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti m e
First
g h the
throu p
loo
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
n d
Seco h the
rou g
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
n d
Seco h the
rou g
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
n d
Seco h the
rou g
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
n d
Seco h the
rou g
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
Third the
rou gh
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
Third the
rou gh
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
Third the
rou gh
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
Third the
rou gh
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
t h
Four h the
ro u g
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
t h
Four h the
ro u g
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
t h
Four h the
ro u g
th
loop
def main():
for i in range(4):
put_beeper()
move()
turn_left()
ti me
t h
Four h the
ro u g
th
loop
!
Done
if/else
if
while lo w
l F
for Loops t ro
Co n
Loops o f
e r
R iv
The
Piech and Sahami, CS106A, Stanford University
while loop
while condition:
statements # note indenting
def move_to_wall():
while front_is_clear():
move() # note indenting
Conditions Karel Can Check For
Test Opposite What it checks
front_is_clear() front_is_blocked() Is there a wall in front of Karel?
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move()
def main():
while front_is_clear():
put_beeper()
move() BUGGY!
!
Done
def main():
while front_is_clear():
put_beeper()
move()
put_beeper() # add final put_beeper
!
Fixed
Not in while loop
def main():
while front_is_clear():
put_beeper()
move()
put_beeper() # add final put_beeper
!
Fixed
Repeat
Process
if/else
if
while lo w
l F
for Loops t ro
Co n
Loops o f
e r
R iv
The
Piech and Sahami, CS106A, Stanford University
if statement
if condition:
statements # note indenting
def safe_pick_up():
if beepers_present():
pick_beeper() # note indenting
Today’s Route
Control
Flow
if/else
if
while lo w
l F
for Loops t ro
Co n
Loops o f
e r
R iv
The
Piech and Sahami, CS106A, Stanford University
if-else statement
if condition:
statements # note indenting
else:
statements # note indenting
def invert_beepers():
if beepers_present():
pick_beeper() # note indenting
else:
put_beeper() # note indenting
You just learned most of
programming “control flow”
def move_to_wall():
while front_is_clear():
move()
def move_to_wall():
while front_is_clear():
move()
def move_to_wall():
while front_is_clear():
move()
def move_to_wall():
while front_is_clear():
move()
def move_to_wall():
while front_is_clear():
move()
def move_to_wall():
while front_is_clear():
move()
def move_to_wall():
while front_is_clear():
move()
def move_to_wall():
while front_is_clear():
move()
def move_to_wall():
while front_is_clear():
move()
ascend_hurdle()
descend_hurdle()
ascend_hurdle()
descend_hurdle()
descend_hurdle()
def descend_hurdle():
turn_right()
move_to_wall()
turn_left()
def jump_hurdle():
ascend_hurdle()
move()
descend_hurdle()
Piech and Sahami, CS106A, Stanford University
A Whole Program:
SteepChaseKarel.py