0% found this document useful (0 votes)
46 views

Lectures - Cip3 - Lecture Slides Control Flow Karel - File

This document discusses control flow in programming and introduces loops using Karel the robot. It provides an example of a for loop that has Karel place beepers in a square pattern by repeating the actions of putting a beeper, moving, and turning left 4 times. The document walks through tracing the program and highlights each iteration of the for loop.

Uploaded by

Zaky Drive
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Lectures - Cip3 - Lecture Slides Control Flow Karel - File

This document discusses control flow in programming and introduces loops using Karel the robot. It provides an example of a for loop that has Karel place beepers in a square pattern by repeating the actions of putting a beeper, moving, and turning left 4 times. The document walks through tracing the program and highlights each iteration of the for loop.

Uploaded by

Zaky Drive
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

Control Flow

Chris Piech and Mehran Sahami


CS106A, Stanford University

Piech and Sahami, CS106A, Stanford University


Recall, Karel’s World

“Streets” run East/West North


3 + + + + +

“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

Piech and Sahami, CS106A, Stanford University


First Lesson in Programming Style
from karel.stanfordkarel import *
"""
File: StepUpKarel.py
-------------------- Multi-line
Karel program, where Karel picks up a beeper, comment
jumps up on a step and drops the beeper off.
"""
def main():
move()
pick_beeper()
move()
turn_left() SOFTWARE ENGINEERING PRINCIPLE:
move() Aim to make programs readable by humans
turn_right()
move()
put_beeper()
move() One line
# Karel turns to the right comment Descriptive
def turn_right():
turn_left() names
turn_left() (snake_case)
turn_left() Piech and Sahami, CS106A, Stanford University
Today’s Goal

1. Code using loops and conditions


2. Trace programs that use loops and conditions

Piech and Sahami, CS106A, Stanford University


Today’s Route
You are here
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
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()

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti m e
First
g h the
throu p
loo

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti m e
First
g h the
throu p
loo

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti m e
First
g h the
throu p
loo

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti m e
First
g h the
throu p
loo

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
n d
Seco h the
rou g
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
n d
Seco h the
rou g
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
n d
Seco h the
rou g
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
n d
Seco h the
rou g
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
Third the
rou gh
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
Third the
rou gh
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
Third the
rou gh
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
Third the
rou gh
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
t h
Four h the
ro u g
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
t h
Four h the
ro u g
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
t h
Four h the
ro u g
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main():
for i in range(4):
put_beeper()
move()
turn_left()

ti me
t h
Four h the
ro u g
th
loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Square

def main(): You need the


for i in range(4):
postcondition of
put_beeper()
a loop to match
move()
the precondition
turn_left()

!
Done

Piech and Sahami, CS106A, Stanford University


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
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?

left_is_clear() left_is_blocked() Is there a wall to Karel’s left?


right_is_clear() right_is_blocked() Is there a wall to Karel’s right?

beepers_present() no_beepers_present() Are there beepers on this corner?

beepers_in_bag() no_beepers_in_bag() Any there beepers in Karel’s bag?

facing_north() not_facing_north() Is Karel facing north?

facing_east() not_facing_east() Is Karel facing east?

facing_south() not_facing_south() Is Karel facing south?

facing_west() not_facing_west() Is Karel facing west?

This is in Chapter 10 of the Karel course reader


Task: Place Beeper Line

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move() BUGGY!

!
Done

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()
put_beeper() # add final put_beeper

!
Fixed
Not in while loop

Piech and Sahami, CS106A, Stanford University


Place Beeper Line

def main():
while front_is_clear():
put_beeper()
move()
put_beeper() # add final put_beeper

!
Fixed

Piech and Sahami, CS106A, Stanford University


Fence Post Problem

Also sometimes called an “Off By One Error”

Piech and Sahami, CS106A, Stanford University


A program executes one
line at a time.

The while loop checks


its condition only at the
start of the code block and
before repeating.

Piech and Sahami, CS106A, Stanford University


Which Loop

Repeat
Process

Know how Don’t know how


many times many times
(definite loop) (indefinite loop)

for Loop while Loop

Piech and Sahami, CS106A, Stanford University


Actual
Actual BugBug from
from Mark Marc II
II Computer

Piech and Sahami, CS106A, Stanford University


Grace Hopper

Piech and Sahami, CS106A, Stanford University


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 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”

Piech and Sahami, CS106A, Stanford University


Today’s Goal

1. Code using loops and conditions


2. Trace programs that use loops and conditions

Piech and Sahami, CS106A, Stanford University


Piech and Sahami, CS106A, Stanford University
Putting it all together
SteepChaseKarel.py

Piech and Sahami, CS106A, Stanford University


Steeple Chase

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()

def move_to_wall():
while front_is_clear():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()

def move_to_wall():
while front_is_clear():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()

def move_to_wall():
while front_is_clear():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()

def move_to_wall():
while front_is_clear():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()

def move_to_wall():
while front_is_clear():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()

def move_to_wall():
while front_is_clear():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()
turn_left()

def move_to_wall():
while front_is_clear():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()
turn_left()

def move_to_wall():
while front_is_clear():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall() You need the
turn_left() postcondition of
a loop to match
the precondition

def move_to_wall():
while front_is_clear():
move()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()
turn_left()

ascend_hurdle()
descend_hurdle()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
turn_left()
while right_is_blocked():
move()
turn_right()
move()
turn_right()
move_to_wall()
turn_left()

ascend_hurdle()
descend_hurdle()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
def ascend_hurdle():
turn_left()
while right_is_blocked():
move() ascend_hurdle()
turn_right() move()
turn_right()
move_to_wall()
turn_left()

descend_hurdle()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
def ascend_hurdle():
turn_left()
while right_is_blocked():
move() ascend_hurdle()
turn_right() move()
descend_hurdle()
def descend_hurdle():
turn_right()
move_to_wall()
turn_left()

Piech and Sahami, CS106A, Stanford University


Focus on One Steeple
def ascend_hurdle():
turn_left()
while right_is_blocked():
move()
turn_right()

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

Piech and Sahami, CS106A, Stanford University

You might also like