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
- Chris piech
- Chris piech stanford
- Karolina piech
- Karolina piech
- Data flow vs control flow
- Control flow and data flow computers
- Transaction flow testing
- Stock control e flow control
- Flow control and error control
- 106 spelled out
- Fahrenheit 451 pages 89-106 summary
- Nick troccoli
- Zodiac telemetry
- Mae 106
- Plt 106
- 106 pal
- 106 signal brigade
- Error n-106
- Section 106 compliance
- Salmo 106
- Fas 106 definition
- Kj 106
- Tc 106
- Iat 106
- Lesson 2 round to estimate products
- Roberts building 106
- 3 108 x 7 942
- 生命聖詩 106
- 100 101 102 103 104 105 106 107 108 109 110
- Prime numbers between 101 and 200
- Sqte-106
- 105×106
- Glucosio 106 a digiuno
- Col 106
- 105 106 107
- Os lusíadas - canto i estâncias 105 e 106
- Cornua of uterus
- Marcus cicero death
- Tempo 106
- Iat 106
- Col 106 amit kumar
- Physics 106
- Ugba 106
- Periods of reactivity newborn
- Soc 106
- Iat 106
- Engl 106 purdue
- Col 106 amit kumar
- Col 106 amit kumar
- 105106
- Denitrogenation atelectasis
- T piece
- Venturi mask flow rate
- Differential pressure flow meters
- Internal versus external flow
- Energy naturally flows from warmer matter to cooler matter.
- Oikos meaning
- Dfd to structure chart
- Data flow structure
- Difference between rotational and irrotational flow
- Internal and external flow
- Cheese making process flowchart
- Pressure-compensated flow control valve symbol
- Udp flow control
- Explain cmp instruction in 8086
- Exceptional control flow
- Repl capture/apply: memory
- Material flow control
- Control flow graph for bubble sort
- Fluid power symbols
- Unity flow chart
- Flow control in database security
- Control valve flow characteristics
- Lactic acid vasodilation
- Control flow guard
- Ipdom
- Linearly independent paths in control flow graph
- Flow divider valve symbol
- Pneumatic dcv
- Pneumatic timer symbol
- Flow control functions in php
- Loop in assembly language 8086
- Cash flow and cost control
- Mbaknol
- Comparators python
- Flow control frame format
- Reset control refers to
- Tcp flow control
- Cpe426
- Outbound
- In sctp, only data chunks consume
- Interrupts execution flow
- Flow control instructions
- Empty statement in sequential block
- Control-flow graph
- Program flow of control without and with interrupts