Lecture 2
Lecture 2
Lecture 2
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
3
Karel’s (Code) World
import stanford.karel.*;
4
Karel’s (Code) World
import stanford.karel.*;
Programming Library We’re Utilizing
public class CollectNewspaperKarel extends SuperKarel {
5
Karel’s (Code) World
import stanford.karel.*;
Name of Program
6
Karel’s (Code) World
import stanford.karel.*;
This is the Type of Program We’re Creating
7
Karel’s (Code) World
import stanford.karel.*;
8
Karel’s (Code) World
import stanford.karel.*; Brackets Noting the Beginning and End of a Code Block
9
A Method
// command 1
// command 2
10
A Method
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();
14
What’s The Pseudocode?
15
What’s The Pseudocode?
place a beeper
move
place a beeper
move
place a beeper
move
place a beeper
16
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?
Much better!
18
Wait! How Do We Repeat?
19
For Loops
// command 1 to repeat!
// command 2 to repeat!
20
For Loops
Our pseudocode:
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++){
23
The Fencepost Pattern
Our Code!
for(int i = 0; i < 3; i++){
Notice: putBeeper();
move();
24
The Fencepost Pattern
1 1 2 2 3 3 4
putBeeper() move() putBeeper() move() putBeeper() move() putBeeper()
Notice:
25
The Fencepost Pattern
1 1 2 2 3 3 4
putBeeper() move() putBeeper() move() putBeeper() move() putBeeper()
Notice:
26
What If…
That’s an awfully long floor....
Our Code!
for(int i = 0; i < 3; i++){
putBeeper();
Our pseudocode: move();
27
While Loops
// command 1 to repeat!
// command 2 to repeat!
28
What Does Karel Know?
29
What Conditions Can Karel Check?
Test Opposite What it checks
31
Tiling the Floor with While Loops
32
Tiling the Floor with While Loops
Our Code!
Our pseudocode:
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()){
35
Let’s Code It!
36
Don’t Forget to Test!
Remember to test early and test
often.
37
What Are We Missing?
38
Tiling the Floor with While Loops
place a beeper
39
Tiling the Floor with While Loops
Our Code!
while(frontIsClear()){
place a beeper
40
Let’s Code It!
41
Tiling the Floor with While Loops
Our Code!
while(frontIsClear()){
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
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!
46
Karel, Let’s Pick Up These Old Tiles!
Uh oh...
47
Tile Clean Up Example
Before:
After:
48
Karel, Let’s Clean Up!
49
Karel, Let’s Clean Up!
50
Karel, Let’s Clean Up!
51
Cleaning Up
Our Code!
Our pseudocode:
52
Cleaning Up
Our Code!
while(frontIsClear()){
53
Let’s Code It!
54
UH OH!
Our Code!
while(frontIsClear()){
pick up beeper
55
UH OH!
Maybe we should add What’s the Pseudocode?
an if statement to check
for a beeper!
56
Cleaning Up with If Statements
if there is a beeper:
pick up beeper
57
Old: Cleaning Up
Our Code!
while(frontIsClear()){
58
Cleaning Up with If Statements
Our Code!
while(frontIsClear()){
If statements check a
condition once!
Phew, that looks
much better... if(conditionIsTrue()){
// command 1!
// command 2!
60
One Last Thing: If-Else
// 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
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