Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line...
Transcript of Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line...
![Page 1: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/1.jpg)
Control FlowChris Gregg
CS106A, Stanford UniversityModified from slides courtesy ofChris Piech and Mehran Sahami
![Page 2: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/2.jpg)
Install PyCharm
Please follow instructions closely.Email Brahm if you have problems.
![Page 3: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/3.jpg)
Using Karel and Assignment 1
• Reading: Should read the “Karel Reader” on class website• Handout #3: “Honor Code”• Handout #4: “Using Karel with PyCharm”
– Tells you how to get started with writing Karel programs• Handout #5: “Assignment 1”
– Set of Karel programs for you to write– Due 10:30am (PDT) on Tuesday, June 30th
• Only use features of Karel in the course reader– No other features of Python may be used in Karel programs!
![Page 4: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/4.jpg)
+ + + + +
+ + + + +
+ + + + +
1
2
3
1 2 3 4 5
North
South
West East
Recall, Karel’s World
“Streets” run East/West
“Avenues” run N
orth/South
• 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
![Page 5: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/5.jpg)
from karel.stanfordkarel import *
"""File: StepUpKarel.py--------------------Karel program, where Karel picks up a beeper,jumps up on a step and drops the beeper off."""
def main():move()pick_beeper()move()turn_left()move()turn_right()move()put_beeper()move()
# Karel turns to the rightdef turn_right():
turn_left()turn_left()turn_left()
First Lesson in Programming Style
Multi-line comment
One line comment
SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable by humans
Descriptive names
(snake_case)
![Page 6: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/6.jpg)
Today’s Goal
1. Code using loops and conditions2. Trace programs that use loops and conditions
![Page 7: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/7.jpg)
Today’s Route
for Loops
while Loops
if
if/else
ControlFlow
The Riv
er of Co
ntrol Flo
w
You are here
![Page 8: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/8.jpg)
for i in range(count):statements # note indenting
def turn_right():for i in range(3):
turn_left() # note indenting
for loop
![Page 9: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/9.jpg)
Place Beeper Square
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 10: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/10.jpg)
Place Beeper Square
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 11: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/11.jpg)
Place Beeper Square
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 12: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/12.jpg)
Place Beeper Square
First time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 13: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/13.jpg)
Place Beeper Square
First time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 14: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/14.jpg)
Place Beeper Square
First time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 15: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/15.jpg)
Place Beeper Square
First time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 16: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/16.jpg)
Place Beeper Square
Second time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 17: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/17.jpg)
Place Beeper Square
Second time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 18: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/18.jpg)
Place Beeper Square
Second time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 19: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/19.jpg)
Place Beeper Square
Second time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 20: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/20.jpg)
Place Beeper Square
Third time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 21: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/21.jpg)
Place Beeper Square
Third time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 22: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/22.jpg)
Place Beeper Square
Third time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 23: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/23.jpg)
Place Beeper Square
Third time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 24: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/24.jpg)
Place Beeper Square
Fourth time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 25: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/25.jpg)
Place Beeper Square
Fourth time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 26: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/26.jpg)
Place Beeper Square
Fourth time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 27: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/27.jpg)
Place Beeper Square
Fourth time
through the
loop
def main():for i in range(4):
put_beeper()move()turn_left()
![Page 28: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/28.jpg)
Place Beeper Square
Done!
def main():for i in range(4):
put_beeper()move()turn_left()
You need the postcondition of a loop to match the precondition
![Page 29: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/29.jpg)
Today’s Route
for Loops
while Loops
if
if/else
ControlFlow
The Riv
er of Co
ntrol Flo
w
![Page 30: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/30.jpg)
while condition:statements # note indenting
def move_to_wall():while front_is_clear():
move() # note indenting
while loop
![Page 31: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/31.jpg)
Conditions Karel Can Check ForTest 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
![Page 32: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/32.jpg)
Task: Place Beeper Line
![Page 33: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/33.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 34: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/34.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 35: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/35.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 36: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/36.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 37: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/37.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 38: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/38.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 39: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/39.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 40: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/40.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 41: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/41.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 42: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/42.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 43: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/43.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 44: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/44.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
![Page 45: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/45.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
Done!
BUGGY!
![Page 46: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/46.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
put_beeper() # add final put_beeper
Fixed!Not in while loop
![Page 47: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/47.jpg)
Place Beeper Line
def main():while front_is_clear():
put_beeper()move()
put_beeper() # add final put_beeper
Fixed!
![Page 48: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/48.jpg)
Fence Post Problem
Also sometimes called an “Off By One Error”
![Page 49: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/49.jpg)
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.
![Page 50: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/50.jpg)
Repeat Process
Which Loop
for Loop while Loop
Know how many times
(definite loop)
Don’t know how many times
(indefinite loop)
![Page 51: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/51.jpg)
Actual Bug from Mark II ComputerActual Bug from Marc II
![Page 52: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/52.jpg)
Grace Hopper
![Page 53: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/53.jpg)
Today’s Route
for Loops
while Loops
if
if/else
ControlFlow
The Riv
er of Co
ntrol Flo
w
![Page 54: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/54.jpg)
if condition:statements # note indenting
def safe_pick_up():if beepers_present():
pick_beeper() # note indenting
if statement
![Page 55: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/55.jpg)
Today’s Route
for Loops
while Loops
if
if/else
ControlFlow
The Riv
er of Co
ntrol Flo
w
![Page 56: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/56.jpg)
if condition:statements # note indenting
else:statements # note indenting
def invert_beepers():if beepers_present():
pick_beeper() # note indentingelse:
put_beeper() # note indenting
if-else statement
![Page 57: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/57.jpg)
You just learned most of programming “control flow”
![Page 58: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/58.jpg)
Today’s Goal
1. Code using loops and conditions2. Trace programs that use loops and conditions
![Page 59: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/59.jpg)
Putting it all togetherSteepChaseKarel.py
![Page 60: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/60.jpg)
Steeple Chase
![Page 61: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/61.jpg)
Focus on One Steeple
![Page 62: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/62.jpg)
Focus on One Steepleturn_left()
![Page 63: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/63.jpg)
Focus on One Steepleturn_left()
![Page 64: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/64.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()
![Page 65: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/65.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()
![Page 66: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/66.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()
![Page 67: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/67.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()
![Page 68: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/68.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()
![Page 69: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/69.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()
![Page 70: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/70.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()
![Page 71: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/71.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()
![Page 72: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/72.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()
![Page 73: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/73.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()
![Page 74: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/74.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()
![Page 75: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/75.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()
![Page 76: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/76.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()move_to_wall()
![Page 77: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/77.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()move_to_wall()
def move_to_wall():while front_is_clear():
move()
![Page 78: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/78.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()move_to_wall()
def move_to_wall():while front_is_clear():
move()
![Page 79: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/79.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()move_to_wall()
def move_to_wall():while front_is_clear():
move()
![Page 80: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/80.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()move_to_wall()
def move_to_wall():while front_is_clear():
move()
![Page 81: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/81.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()move_to_wall()
def move_to_wall():while front_is_clear():
move()
![Page 82: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/82.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()move_to_wall()
def move_to_wall():while front_is_clear():
move()
![Page 83: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/83.jpg)
Focus on One Steepleturn_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()
![Page 84: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/84.jpg)
Focus on One Steepleturn_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()
![Page 85: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/85.jpg)
Focus on One Steepleturn_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()
You need the postcondition of a loop to match the precondition
![Page 86: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/86.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()move_to_wall()turn_left()
ascend_hurdle()
descend_hurdle()
![Page 87: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/87.jpg)
Focus on One Steepleturn_left()while right_is_blocked():
move()turn_right()move()turn_right()move_to_wall()turn_left()
ascend_hurdle()
descend_hurdle()
![Page 88: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/88.jpg)
Focus on One Steeple
move()turn_right()move_to_wall()turn_left()
descend_hurdle()
def ascend_hurdle():turn_left()while right_is_blocked():
move()turn_right()
ascend_hurdle()
![Page 89: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/89.jpg)
Focus on One Steeple
move()ascend_hurdle()
descend_hurdle()
def ascend_hurdle():turn_left()while right_is_blocked():
move()turn_right()
def descend_hurdle():turn_right()move_to_wall()turn_left()
![Page 90: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/90.jpg)
Focus on One Steepledef 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()
![Page 91: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2d2eb2e391d138f639c605/html5/thumbnails/91.jpg)
A Whole Program:SteepChaseKarel.py