Control Flow Chris Piech CS 106 A Stanford































































































- Slides: 95
Control Flow Chris Piech CS 106 A, Stanford University Piech, CS 106 A, Stanford University
CS 106 A Piech, CS 106 A, Stanford University
Karel the Robot s * While Karel is in Java, when you program your Karel assignment we ask that you stick to the concepts in the course reader Piech, CS 106 A, Stanford University
First Challenge 3 + + 2 + + + + 1 2 3 4 Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); turn. Left(); move(); turn. Right(); This is the program's move(); source code put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); turn. Left(); This piece of the program's move(); source code is called a turn. Right(); method. move(); put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); x pick. Beeper(); move(); This line of code gives the turn. Left(); name of the method move(); (here, run) turn. Right(); move(); put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); This line of code gives the turn. Left(); name of the method move(); (here, turn. Right) turn. Right(); move(); put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); turn. Left(); move(); turn. Right(); This is called an import statement. move(); It tells Java what Karel is. put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); This is called a turn. Left(); code block move(); turn. Right(); move(); put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); turn. Left(); move(); turn. Right(); move(); put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); turn. Left(); move(); turn. Right(); move(); put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); turn. Left(); move(); turn. Right(); move(); put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); turn. Left(); move(); turn. Right(); move(); put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); turn. Left(); move(); turn. Right(); The run method is “public” so that move(); Eclipse can call it. put. Beeper(); move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Anatomy of a Program import stanford. karel. *; public class Our. Karel. Program extends Karel { public void run() { move(); pick. Beeper(); move(); turn. Left(); move(); turn. Right(); The turn. Right method is “private” to move(); indicate it is only visible to our put. Beeper(); current program. move(); } } private void turn. Right() { turn. Left(); } Piech, CS 106 A, Stanford University
Piech, CS 106 A, Stanford University
Method Definition private void name() { statements in the method body } ew n a ls ds e d r a a This nd to K a ry m a l m u b co a c o v Piech, CS 106 A, Stanford University
For loops, While loops, If/Else statements Piech, CS 106 A, Stanford University
Place 99 beepers? 99 Piech, CS 106 A, Stanford University
Place 99 beepers public class Place 99 Beepers extends Super. Karel { public void run() { move(); for(int i = 0; i < 99; i++) { put. Beeper(); } move(); } This “for loop” repeats } the code in its “body” 99 times Piech, CS 106 A, Stanford University
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } th e m i t t p o Firs o l he t h g rou Piech, CS 106 A, Stanford University
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } th e m i t t p o Firs o l he t h g rou Piech, CS 106 A, Stanford University
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } th e m i t t p o Firs o l he t h g rou Piech, CS 106 A, Stanford University
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } th e m i t t p o Firs o l he t h g rou Piech, CS 106 A, Stanford University
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University me i t d n Seco the loop gh u o r th
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University me i t d n Seco the loop gh u o r th
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University me i t d n Seco the loop gh u o r th
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University me i t d n Seco the loop gh u o r th
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University e m i t Third he loop t h g u thro
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University e m i t Third he loop t h g u thro
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University e m i t Third he loop t h g u thro
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University e m i t Third he loop t h g u thro
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University e m i t h t Four the loop gh u o r th
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University e m i t h t Four the loop gh u o r th
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University e m i t h t Four the loop gh u o r th
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University e m i t h t Four the loop gh u o r th
Place Beeper Square public class Beeper. Square extends Super. Karel { public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); turn. Left(); } } } Piech, CS 106 A, Stanford University
Exciting! Piech, CS 106 A, Stanford University
Aside: Super Karel import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // super karel has a few more commands turn. Right(); turn. Around(); paint. Corner(BLUE); } put. Beeper(); move(); } Piech, CS 106 A, Stanford University
Next task Piech, CS 106 A, Stanford University
Place Beeper Line Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { } public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); } } Piech, CS 106 A, Stanford University
Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { } public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); } } Piech, CS 106 A, Stanford University
Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { } public void run() { for(int i = 0; i < 4; i++) { put. Beeper(); move(); } } What we want Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { for(int i = 0; i < 3; i++) { put. Beeper(); move(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { for(int i = 0; i < 3; i++) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Actual Bug from Marc II Actual Bug from Mark II Computer Piech, CS 106 A, Stanford University
Grace Hopper Piech, CS 106 A, Stanford University
Don’t Know World Size ? Piech, CS 106 A, Stanford University
Work in Any World Before After Piech, CS 106 A, Stanford University
While Loop Piech, CS 106 A, Stanford University
While Loop import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(condition) { code to repeat } } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Place Beeper Line import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // example while loop while(front. Is. Clear()) { put. Beeper(); move(); } // extra put beeper put. Beeper(); } } Piech, CS 106 A, Stanford University
Piech, CS 106 A, Stanford University
Common misconception: Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University This is incredibly important!
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Place Beeper Line: Redux import stanford. karel. *; public class Beeper. Line extends Super. Karel { public void run() { // place a first beeper put. Beeper(); // example while loop while(front. Is. Clear()) { move(); put. Beeper(); } } } Piech, CS 106 A, Stanford University
Which Loop Repeat Process Don’t know how many times Know how many times For Loop While Loop Piech, CS 106 A, Stanford University
What if you only want to repeat one time? Piech, CS 106 A, Stanford University
If statement Piech, CS 106 A, Stanford University
If Statement import stanford. karel. *; public class If. Example extends Super. Karel { public void run() { // example of an if statement if(condition) { code to run if condition is true } } } Piech, CS 106 A, Stanford University
If Statement import stanford. karel. *; public class If. Example extends Pretend{ public void run() { // example of an if statement if(you. Like. Beyonce()) { make. Some. Noise(); } } } Piech, CS 106 A, Stanford University
If Statement import stanford. karel. *; public class If. Example extends Super. Karel{ public void run() { safe. Move(); } private void safe. Move() { if(front. Is. Clear()) { move(); } } } Piech, CS 106 A, Stanford University
If / Else Statement import stanford. karel. *; public class If. Example extends Super. Karel{ public void run() { invert. Beeper(); } private void invert. Beeper() { if(beepers. Present()) { pick. Beeper(); } else { put. Beeper(); } } } Piech, CS 106 A, Stanford University
The Full Karel Piech, CS 106 A, Stanford University
Random Painter Before: After: Piech, CS 106 A, Stanford University
You just learned most of programming “contro flow” Piech, CS 106 A, Stanford University