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

Lecture 2

The lecture covers control flow concepts in programming using Karel, including methods, for loops, while loops, and if statements. Students are encouraged to sign up for sections and complete a personal introduction form. Homework includes setting up Eclipse and signing up for a course section by the deadline.

Uploaded by

VAIBHAV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 2

The lecture covers control flow concepts in programming using Karel, including methods, for loops, while loops, and if statements. Students are encouraged to sign up for sections and complete a personal introduction form. Homework includes setting up Eclipse and signing up for a course section by the deadline.

Uploaded by

VAIBHAV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Control Flow with Karel

Lecture 2

CS106A, Summer 2019


Sarai Gould & Laura Cruz-Albrecht
Quick Announcements

Sign up for sections by today at 5pm.

Don’t forget to fill out the form to tell us about yourself! It’s on
Piazza, the lecture slides, and the CS106a website.

2
Plan for Today

● Karel’s (Code) World


hi class!
● Review: Our First Method!
● For Loops and Fence Posts
● While Loops
● If Statements

3
Karel’s (Code) World
import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {

public void run(){

// Your main program here!

// Create new methods for Karel down here!

4
Karel’s (Code) World
import stanford.karel.*;
Programming Library We’re Utilizing
public class CollectNewspaperKarel extends SuperKarel {

public void run(){

// Your main program here!

// Create new methods for Karel down here!

5
Karel’s (Code) World
import stanford.karel.*;
Name of Program

public class CollectNewspaperKarel extends SuperKarel {

public void run(){

// Your main program here!

// Create new methods for Karel down here!

6
Karel’s (Code) World
import stanford.karel.*;
This is the Type of Program We’re Creating

public class CollectNewspaperKarel extends SuperKarel {

public void run(){

// Your main program here!

// Create new methods for Karel down here!

7
Karel’s (Code) World
import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {


“run” Method - All of Our Programs Will Have This!
public void run(){

// Your main program here!

// Create new methods for Karel down here!

8
Karel’s (Code) World
import stanford.karel.*; Brackets Noting the Beginning and End of a Code Block

public class CollectNewspaperKarel extends SuperKarel {

public void run(){

// Your main program here!

// Create new methods for Karel down here!

9
A Method

A method is a set of new


instructions we’ve created!
/* Comment describing method */
private void nameOfMethod(){

// command 1
// command 2

10
A Method

A method is a set of new


instructions we’ve created!
/* Allows Karel to turn right */
private void turnRight(){

turnLeft();
turnLeft();
turnLeft();

11
Do It Again, Karel!

Karel recently got a job tiling floors. Can we help Karel fill this
WHOLE world with tiles (aka beepers)?

12
Reminder: What Can Karel Do?
Karel Can:

move();

turnLeft();

putBeeper();

pickBeeper();

And now: turnRight();


13
What’s The Plan?

What’s the Plan?

Pseudocode or “fake code”


is common term used to
describe a programming
plan.

14
What’s The Pseudocode?

What’s the Pseudocode?

15
What’s The Pseudocode?

What’s the Pseudocode?

place a beeper
move
place a beeper
move
place a beeper
move
place a beeper

16
What’s The Pseudocode?

What’s the Pseudocode?

place a beeper
move Seems repetitive…
place a beeper How can we
move simplify it?
place a beeper
move
place a beeper

17
What’s The Pseudocode?

What’s the Pseudocode?

repeat three times:


place a beeper
move
place a beeper

Much better!

18
Wait! How Do We Repeat?

Ummmm, what do you mean


by repeat? What does that
code look like?

19
For Loops

For Loops repeat something


a specific number of times!
for(int i = 0; i < num_times; i++){

// command 1 to repeat!
// command 2 to repeat!

// code out here is NOT repeated!

20
For Loops

Our pseudocode:

repeat three times:


place a beeper
move
place a beeper

21
For Loops

Our Code!
for(int i = 0; i < 3; i++){

Our pseudocode:
}
repeat three times:
place a beeper
move
place a beeper

22
For Loops

Our Code!
for(int i = 0; i < 3; i++){

Our pseudocode: putBeeper();


move();

repeat three times: }

place a beeper putBeeper();


move
place a beeper

23
The Fencepost Pattern

Our Code!
for(int i = 0; i < 3; i++){

Notice: putBeeper();
move();

We put a beeper 4 times. }


We moved 3 times. putBeeper();

24
The Fencepost Pattern

1 1 2 2 3 3 4
putBeeper() move() putBeeper() move() putBeeper() move() putBeeper()
Notice:

We put a beeper 4 times.


We moved 3 times.

25
The Fencepost Pattern

1 1 2 2 3 3 4
putBeeper() move() putBeeper() move() putBeeper() move() putBeeper()
Notice:

We put a beeper n+1 times.


We moved n times.

26
What If…
That’s an awfully long floor....

Our Code!
for(int i = 0; i < 3; i++){

putBeeper();
Our pseudocode: move();

What if we don’t }know how many


repeat three times:tiles Karel will have to lay down?
putBeeper();
place a beeper
move
place a beeper

27
While Loops

While Loops repeat until a


condition is no longer met!
while(conditionIsTrue()){

// command 1 to repeat!
// command 2 to repeat!

// the condition isn’t true anymore,


// so our code exited the while loop.
// code out here is NOT repeated!

28
What Does Karel Know?

What conditions can I


check? Can I check if
anything is true?

29
What Conditions Can Karel Check?
Test Opposite What it checks

frontIsClear() frontIsBlocked() Is there a wall in front of Karel?

leftIsClear() leftIsBlocked() Is there a wall to Karel’s left?

rightIsClear() rightIsBlocked() Is there a wall to Karel’s right?

beepersPresent() noBeepersPresent() Are there beepers on this corner?

beepersInBag() noBeepersInBag() Any there beepers in Karel’s bag?

facingNorth() notFacingNorth() Is Karel facing north?

facingEast() notFacingEast() Is Karel facing east?

facingSouth() notFacingSouth() Is Karel facing south?

facingWest() notFacingWest() Is Karel facing west?


30
Tiling the Floor with While Loops

What’s the Pseudocode?

31
Tiling the Floor with While Loops

What’s the Pseudocode?

while nothing is in front of Karel:


place a beeper
move

32
Tiling the Floor with While Loops

Our Code!

Our pseudocode:

while nothing is in front of Karel:


place a beeper
move

33
Tiling the Floor with While Loops

Our Code!
while(frontIsClear()){

Our pseudocode:
}
while nothing is in front of Karel:
place a beeper
move

34
Tiling the Floor with While Loops

Our Code!
while(frontIsClear()){

Our pseudocode: putBeeper();


move();

while nothing is in front of Karel: }


place a beeper
move

35
Let’s Code It!

36
Don’t Forget to Test!
Remember to test early and test
often.

Test parts of your program as you go


along to catch bugs along the way!

37
What Are We Missing?

What’s the Pseudocode?

while nothing is in front of Karel:


place a beeper
move

38
Tiling the Floor with While Loops

What’s the Pseudocode?

while nothing is in front of Karel:


place a beeper
move

place a beeper

39
Tiling the Floor with While Loops

Our Code!
while(frontIsClear()){

Our pseudocode: putBeeper();


move();

while nothing is in front of Karel: }


place a beeper putBeeper();
move

place a beeper
40
Let’s Code It!

41
Tiling the Floor with While Loops

Our Code!
while(frontIsClear()){

Our pseudocode: putBeeper();


move();
Looks like another fencepost
while nothing is in front of Karel:
program! }
place a beeper putBeeper();
move

place a beeper
42
While Loop or For Loop?
How do I know when I
should use a while loop
or a for loop?

43
While Loop or For Loop?

Process to Repeat

Know how Don’t know how


many times many times

For Loop While Loop

44
What Is It Now, Karel?
All of this repeating is
making me tired!
What if I only want to check
for something only once?

45
If Statements

If statements check a
condition once!
Phew, that looks
much better... if(conditionIsTrue()){

// command 1!
// command 2!

// we have left the if statement.


// code out here happens no matter what!

46
Karel, Let’s Pick Up These Old Tiles!

Uh oh...

Karel must pick up all of the old


tiles that were left behind!

47
Tile Clean Up Example
Before:

After:

48
Karel, Let’s Clean Up!

What’s the Pseudocode?

49
Karel, Let’s Clean Up!

What’s the Pseudocode?

while nothing is in front of Karel:


pick up beeper
move

50
Karel, Let’s Clean Up!

What’s the Pseudocode?

while nothing is in front of Karel:


pick up beeper
move
pick up beeper

51
Cleaning Up

Our Code!

Our pseudocode:

while nothing is in front of Karel:


pick up beeper
move
pick up beeper

52
Cleaning Up

Our Code!
while(frontIsClear()){

Our pseudocode: pickBeeper();


move();

while nothing is in front of Karel: }


pick up beeper pickBeeper();
move
pick up beeper

53
Let’s Code It!

54
UH OH!

Our Code!
while(frontIsClear()){

Our pseudocode: pickBeeper();


move();
You can’t pick
while nothing is in front of Karel: }
up a beeper if
pick up beeper pickBeeper(); there isn’t one
move there!

pick up beeper

55
UH OH!
Maybe we should add What’s the Pseudocode?
an if statement to check
for a beeper!

while nothing is in front of Karel:


pick up beeper
move
pick up beeper

56
Cleaning Up with If Statements

What’s the Pseudocode?


That looks better.
while nothing is in front of Karel:
if there is a beeper:
pick up beeper
move

if there is a beeper:
pick up beeper
57
Old: Cleaning Up

Our Code!
while(frontIsClear()){

Our pseudocode: pickBeeper();


move();

while nothing is in front of Karel: }


pick up beeper pickBeeper();
move
pick up beeper

58
Cleaning Up with If Statements

Our Code!
while(frontIsClear()){

Our pseudocode: if(beepersPresent()){


pickBeeper();
}
while nothing is in front of Karel: move();
if there is a beeper:
}
pick up beeper
move if(beepersPresent()){
pickBeeper();
}
if there is a beeper:
pick up beeper
59
If Statements

If statements check a
condition once!
Phew, that looks
much better... if(conditionIsTrue()){

// command 1!
// command 2!

// we have left the if statement.


// code out here happens no matter what!

60
One Last Thing: If-Else

The else code only runs if the


condition is not true!
if(conditionIsTrue()){

// command 1 if conditionIsTrue()!
// command 2 if conditionIsTrue()!

} else {

// command 1 if conditionIsFalse()!
// command 2 if conditionIsFalse()!

}
// code out here happens no matter what!
61
Plan for Today

● Karel’s (Code) World


● Review: Our First Method!
● For Loops and Fence Posts
● While Loops
● If Statements

Homework:
● Set up Eclipse (instructions on course website)
● Due 5pm Today: Sign up for section on course website
● Assignment 0: Tell us about yourself - https://bit.ly/2X0Pmzz62

You might also like