while loop Condition Statement list How do I know the condition will become false? while (mark.isFacingNorth()) { mark.pickThing(); mark.move(); } Condition Statement list T while (mark.getStreet()!=10) { mark.move(); } F We should make sure the statement list will work towards the exit condition of the loop. But how? Drink more coffee! Think clear! 5/12/2019 ITK 168
Variables are name of memory locations where values are stored. int j; int i = 1; j = i; City ny; // ny = i; // this is illegal ny = new City(); Robot mark; RobotSE tom = new RobotSE(ny,0,0,Direction.EAST); mark = tom; // tom = mark; // this is illegal We have to decide for a variable: the name the type the value This may be changed later 5/12/2019 ITK 168
Variables We can use variable to control a loop. // the following code will ask the robot to keep moving // until it picks up 10 things. RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); int count=0; while (count != 10) { if (mark.canPickThing()) { mark.pickThing(); count ++; } else { mark.move(); How do I know the robot eventually will have 10 things? 5/12/2019 ITK 168
To guarantee exit, we may provide an upper bound of effort How about, pick 10 things, if possible, with 100 steps? RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); int count=0, distance=0; while (count != 10 && distance < 100) { if (mark.canPickThing()) { mark.pickThing(); count ++; } else { mark.move(); distance++; 5/12/2019 ITK 168
while loop for loop Statement Condition Statement list list repeat n times Statement list < n F = n while (Condition) { Statement list } for (???;???;???) { Statement list } 5/12/2019 ITK 168
don’t forget “;” be careful Another form of loop: do loop do { Statement list } while (condition); Statement list don’t forget “;” be careful Condition T This is not uncommon (forget the author’s comments on page 243) F E.g., ITK’s student has to take ITK168 until he/she gets a C or better. 5/12/2019 ITK 168
Another example of using do-loop Ask the user to input y (yes) or n (no), where y and n are the only two valid inputs for the program to proceed. This way, we avoid a great chance of accidentally pressing the keyboard. String answer; do { answer = JOptionPane.showInputDialog(“y or n?”); } while (!answer.equals(“y”) && !answer.equals(“n”)); String answer; answer = “x”; while (!answer.equals(“y”) && !answer.equals(“n”)) { answer = JOptionPane.showInputDialog(“y or n?”); } 5/12/2019 ITK 168
Primality Test A prime number is a positive integer that cannot be factorized, i.e., no numbers other that 1 and itself can divide it. Is (893 % 2 == 0) ? Is (893 % 3 == 0) ? Is (893 % 4 == 0) ? Is (893 % 5 == 0) ? Is (893 % 6 == 0) ? . Is (893 % 892 == 0) ? is 893 a prime? This test is silly but can do the job! We can write a loop to test these. 5/12/2019 ITK 168
Primality Test int p,r,i; // r: remainder, i: factor do { p = Integer.parseInt( JOptionPane.showInputDialog(“input an number:)); } while (p <= 1); i=1; do { i++; r = p % i; // r is the remainder of p divided by i } while (i < p && r != 0); if (i == p) System.out.println(“a prime number.”); else System.out.println(“not a prime number.”); System.out.println(“The least factor is ”+i); } 5/12/2019 ITK 168
The break statement in a loop will force the program to jump out of the loop immediately. Break a loop int i = 2; do { r = p % i; i++; } while (i < p && r != 0); int i = 2; do { r = p % i; if (r == 0) break; i++; } while (i < p); 5/12/2019 ITK 168
Break a loop import javax.swing.JOptionPane; .... public static void marryAsk() { String answer; do { answer = JOptionPane.showInputDialog( "Would you marry me? (y/n)"); if (answer.equals("Y") || answer.equals("y")) "Really? (y/n)"); } while (!answer.equals("Y") && !answer.equals("y")); JOptionPane.showMessageDialog(null, "Great!!"); } if (answer.equals("F") || answer.equals("f")) break; 5/12/2019 ITK 168
The Loops with multiple breaking points .... while (......) { if (....) break; ..... } Statement list Condition T F Spaghetti code? Why spaghetti code is bad? Well, this is not spaghetti code! 5/12/2019 ITK 168
The Loops with multiple breaking points This is Spaghetti code? This is not spaghetti code! All paths come to the same point Multiple-break-loop does not cause the trouble as the spaghetti code does 5/12/2019 ITK 168
1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 5/12/2019 ITK 168
Continue in a loop The continue statement in a loop will force the program to check the loop condition immediately. RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); while (mark.getAvenue() <= 10) { if (mark.frontIsClear()) { mark.move(); continue; } mark.turnRight(); mark.turnLeft(); 5/12/2019 ITK 168
Continue in a loop The continue statement in a loop will force the program to check the loop condition immediately. String answer; do { answer = JOptionPane.showInputDialog( "Would you marry me? (y/n)"); if (!answer.equals("Y") && !answer.equals("y")) continue; JOptionPane.showMessageDialog(null, "Great!!"); break; } while (true); 5/12/2019 ITK 168
Euclid Algorithm: finding the greatest common divisor int euclidAlgorithm(int a, int b) { int r = a % b; while (r != 0) a = b; b = r; r = a % b; } JOptionPane.showMessageDialog(null,“The GCD is”+b); return b; 5/12/2019 ITK 168
Bottom Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.canPickThing()) { mark.pickThing(); mark.turnAround(); } else { mark.putThing(); } RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.canPickThing()) { mark.pickThing(); } else { mark.putThing(); } mark.turnAround(); 5/12/2019 ITK 168
Top Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.canPickThing()) { mark.turnAround(); mark.pickThing(); } else { mark.move(); } RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); mark.turnAround(); if (mark.canPickThing()) { mark.pickThing(); } else { mark.move(); } 5/12/2019 ITK 168
Top Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.countThingsInBackpack()>2) { mark.putThing(); mark.move(); } else { mark.turnArounde(); } Top Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); mark.putThing(); if (mark.countThingsInBackpack()>2) { mark.move(); } else { mark.turnAround(); } 5/12/2019 ITK 168
Top Factoring RobotSE mark = new RobotSE(ny,0,0,....); ... if (mark.isFacingNorth()) { mark.turnAround(); mark.pickThing(); } else { mark.move(); } RobotSE mark = new RobotSE(ny,0,0,....); ... mark.turnAround(); if (mark.isFacingNorth()) { mark.pickThing(); } else { mark.move(); } 5/12/2019 ITK 168
Top Factoring int a=1,i=0; ... if (i==0) { i++; a = a+i; } else { 5/12/2019 ITK 168