flow control in java
DESCRIPTION
Flow Control in Java. Controlling which instruction to execute next. Sequential Similar to walking, one step after another Branching Similar to a fork in the road Depending on the destination, you choose one way or the other, not both Repetition - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/1.jpg)
Flow Control in Java
![Page 2: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/2.jpg)
Controlling which instruction to execute next
Sequential Similar to walking, one step after another
Branching Similar to a fork in the road
Depending on the destination, you choose one way or the other, not both
Repetition Similar to running on a track in the Olympics
Repeating the same track in a loop
![Page 3: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/3.jpg)
Sequential
x = 1;x = x + 1;
As expected First instruction first Second instruction second
What if we swap the two instructions?
That is Instructions cannot be in arbitrary order
![Page 4: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/4.jpg)
Branching (Conditional Statements)
if ( x < y ) // boolean conditionx = x + 1; // execute if true
elsey = y * 10; // execute if false
![Page 5: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/5.jpg)
Second/Else Branch is Optional
if ( x < y ) // boolean conditionx = x + 1; // execute if true
![Page 6: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/6.jpg)
Multiple Instructions in One Branch
if ( x < y ) { // note the matching braces x = x + 1; y = y – x; }
else { y = y * 10; x = y / x; }
![Page 7: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/7.jpg)
Nested Branching
if ( x < y ) { x = x + 1; if ( y > 10) y = y – x;}
else { y = y * 10; x = y / x;}
![Page 8: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/8.jpg)
Cascaded Branching
if (score >= 90) grade = ’A’;else if (score >= 80) grade = ’B’;else if (score >= 70) grade = ’C’;else if (score >= 60) grade = ’D’;else grade = ’F’;
![Page 9: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/9.jpg)
Version 2: always correct answer?
if (score >= 90) grade = ’A’;if (score >= 80) grade = ’B’;if (score >= 70) grade = ’C’;if (score >= 60) grade = ’D’;if (score < 60) grade = ’F’;
![Page 10: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/10.jpg)
Version 3: always correct answer?
if (score >= 90) grade = ’A’;if (score >= 80 && score < 90) grade = ’B’;if (score >= 70 && score < 80) grade = ’C’;if (score >= 60 && score < 70) grade = ’D’;if (score < 60) grade = ’F’;
![Page 11: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/11.jpg)
Repetition (looping)
for “counting” loops frequently (sometimes inappropriately) used
while general loops most flexible
do-while (“Repeat until”) at least once least used
![Page 12: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/12.jpg)
ICU
Initialize (start) What is the initial/starting condition?
Continue (or stop) When to continue/stop? In what condition does it continue/stop?
Update How to update the condition?
If ICU is not carefully designed (common mistake) your program will be in ICU
![Page 13: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/13.jpg)
Counting loop – 1, 2, 3, … 10
for (int num = 1; num <= 10; num++) System.out.println(num);
for (initialize; continue; update) body -- instruction(s) to be repeated
Continue --boolean (continue if true, stop if false)
![Page 14: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/14.jpg)
How about from 55 to 123?
for (int num = ?; ?; ?) System.out.println(num);
![Page 15: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/15.jpg)
How about from 55 to 123?
for (int num = 55; num <= 123; num++) System.out.println(num);
![Page 16: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/16.jpg)
How about 10 numbers from 55?
for (int num = ?; ?; ?) System.out.println(num);
![Page 17: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/17.jpg)
How about 10 numbers from 55?
// version 1?for (int num = 55; num <= 64; num++) System.out.println(num);// version 2?for (int num = 55; num <= 65; num++) System.out.println(num);// version 3?for (int num = 55; num < 65; num++) System.out.println(num);
![Page 18: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/18.jpg)
How about 10 even numbers from 2?
for (int num = ?; ?; ?) System.out.println(num);
![Page 19: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/19.jpg)
How about 10 even numbers from 2?
// version 1?for (int num = 2; num <= 20; num=num+2) System.out.println(num);// version 2?for (int num = 2; num <= 18; num=num+2) System.out.println(num);// version 3?for (int num = 2; num < 20; num=num+2) System.out.println(num);
![Page 20: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/20.jpg)
How about 10 even numbers down from 100?
for (int num= ? ; ?; ?) System.out.println(num);
![Page 21: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/21.jpg)
How about 10 even numbers down from 100?
// version 1?for (int num=100; num >= 80; num=num-2) System.out.println(num);// version 2?for (int num=100; num >= 82; num=num-2) System.out.println(num);// version 3?for (int num=100; num > 82; num=num-2) System.out.println(num);
![Page 22: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/22.jpg)
Anything that is strange?
for (int num=10; num < 10; num++) System.out.println(num);
![Page 23: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/23.jpg)
Anything that is strange?
for (int num=10; num < 10; num++) System.out.println(num);
continue is never true, body never executes
![Page 24: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/24.jpg)
Anything that is strange?
for (int num=10; num >= 10; num++) System.out.println(num);
![Page 25: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/25.jpg)
Anything that is strange?
for (int num=10; num >= 10; num++) System.out.println(num);
Continueis always true, infinite loop(eventually stops since int has an upper limit
and num overflows)
![Page 26: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/26.jpg)
Finding Sum of 1 to 10
int sum = 0;for (int num = 1; num <= 10; num++) sum = sum + num;
![Page 27: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/27.jpg)
Finding Sum of 1 to 10
int sum = 0;for (int num = 1; num <= 10; num++) sum = sum + num;
// --- version 2 ? ---int sum = 0;for (int num = 1; num < 10; num++) sum = sum + num;
![Page 28: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/28.jpg)
Sum of first 10 even numbers
int sum = 0;for (int num = ?; ? ; ?) sum = sum + num;
![Page 29: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/29.jpg)
Sum of first 10 even numbers
int sum = 0;for (int num = 0; num <= 18; num = num + 2) sum = sum + num;
![Page 30: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/30.jpg)
Printing a Line of 5 Stars
for (int star = 1; star <= 5; star++) { System.out.print(’*’); }System.out.println(); // new line// --- output: ---*****
![Page 31: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/31.jpg)
Printing a Line of 5 Stars
for (int star = 1; star <= 5; star++) { System.out.print(’*’); }System.out.println(); // new line// --- version 2 ? --- for (int star = 0; star < 5; star++) { System.out.print(’*’); }System.out.println(); // new line
![Page 32: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/32.jpg)
4x5 Rectangle of Stars
??
for (int star = 1; star <= 5; star++)//5 stars { System.out.print(’*’); } System.out.println();// --- Output: ---********************
![Page 33: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/33.jpg)
4x5 Rectangle of Stars – nested loop
for (int line = 1; line <= 4; line++) //4 lines { for (int star = 1; star <= 5; star++)//5 stars { System.out.print(’*’); } System.out.println(); }// --- Output: ---********************
![Page 34: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/34.jpg)
Triangle of Stars
***************
![Page 35: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/35.jpg)
“While” loop
int num = 1, sum = 0;while (num <= 10) { sum = sum + num; num++; }
initializewhile (continue) // repeat if continue is true {
update }
![Page 36: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/36.jpg)
A program with an exit command
boolean exit = false; while (exit == false) { // do stuff
if ( //exit command is entered ) exit = true; }
![Page 37: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/37.jpg)
Is num a prime number?
![Page 38: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/38.jpg)
Is num a prime number?
Definition Only divisible by 1 or itself
Check factors between 2 and num – 1 to see if num is divisible by factor
Don’t need check factors larger than num
![Page 39: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/39.jpg)
Checking Different Factors
Initialize Start factor with 2
Continue Factor is less than num num is not divisible by factor
Update Increment factor
![Page 40: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/40.jpg)
in Java
// I: start factor with 2int factor = 2;
// C: factor less than num and // num not divisible by factorwhile (factor < num && num % factor != 0) { factor++; // U: increment factor }
![Page 41: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/41.jpg)
But how do we print out the answer?
// I: start factor with 2int factor = 2;
// C: factor less than num and // num not divisible by factorwhile (factor < num && num % factor != 0) { factor++; // U: increment factor }
System.out.println(?);
![Page 42: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/42.jpg)
Print the answer as well
// I: start factor with 2int factor = 2;
// C: factor less than num and // num not divisible by factorwhile (factor < num && num % factor != 0) { factor++; // U: increment factor }if (factor == num) // not divisible by smaller factors System.out.println(“prime”);else System.out.println(“not prime”);
![Page 43: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/43.jpg)
“Do-While” loop
Execute the loop body at least oncecontinue is checked after the loop body is
executed
initializedo {
update }while (continue); // repeat if continue is true // note the semicolon at the end
![Page 44: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/44.jpg)
Checking input
int numTickets = 0;do { System.out.print(”Please enter # of tickets: ”); numtickets = keyboard.nextInt(); }while (numTickets <= 0);
![Page 45: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/45.jpg)
Checking Password
String username = ””, password = ””;
do { System.out.print(”Please enter username: ”); username = keyboard.next(); System.out.print(”Please enter password: ”); password = keyboard.next(); }while (!valid(username, password));
![Page 46: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/46.jpg)
How to add at most 3 Trials?
String username = ””, password = ””;
do { System.out.print(”Please enter username: ”); username = keyboard.next(); System.out.print(”Please enter password: ”); password = keyboard.next(); }while (!valid(username, password));
![Page 47: Flow Control in Java](https://reader036.vdocuments.net/reader036/viewer/2022062809/56815783550346895dc51a5f/html5/thumbnails/47.jpg)
At most 3 Trials
String username = ””, password = ””;int trials=0;do { System.out.print(”Please enter username: ”); username = keyboard.next(); System.out.print(”Please enter password: ”); password = keyboard.next(); trials++; }while (!valid(username, password) && trials < 3);