1 / 22

while loop

How do I know the condition will become false?. while loop. while ( mark.isFacingNorth() ) { mark.pickThing(); mark.move(); }. Condition. Statement list. T. while ( mark.getStreet()!=10 ) { mark.move(); }. F.

jamalh
Download Presentation

while loop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How do I know the condition will become false? while loop 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! ITK 168

  2. Variables are name of memory locations where values are stored. Variables 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 ITK 168

  3. We can use variable to control a loop. Variables // 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? ITK 168

  4. 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++; } } ITK 168

  5. for loop while loop Condition Statement list T repeat n times Statement list < n F = n while (Condition) { Statement list } for (???;???;???) { Statement list } ITK 168

  6. doloop Another form of 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. ITK 168

  7. 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?”); } ITK 168

  8. 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. ITK 168

  9. 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); } ITK 168

  10. Break a loop The break statement in a loop will force the program to jump out of the loop immediately. 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); ITK 168

  11. Break a loop import javax.swing.JOptionPane; .... .... publicstaticvoid marryAsk() { String answer; do { answer = JOptionPane.showInputDialog( "Would you marry me? (y/n)"); if (answer.equals("Y") || answer.equals("y")) answer = JOptionPane.showInputDialog( "Really? (y/n)"); } while (!answer.equals("Y") && !answer.equals("y")); JOptionPane.showMessageDialog(null, "Great!!"); } if (answer.equals("F") || answer.equals("f")) break; ITK 168

  12. The Loops with multiple breaking points .... .... while (......) { .... if (....) break; .... if (....) break; .... if (....) break; .... if (....) break; ..... ..... } .... .... Statement list Condition T F Spaghetti code? Why spaghetti code is bad? Well, this is not spaghetti code! ITK 168

  13. 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 ITK 168

  14. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 1 2 3 4 5 6 7 8 9 10 11 ITK 168

  15. 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.move(); mark.turnLeft(); } ITK 168

  16. 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); ITK 168

  17. 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; } ITK 168

  18. Bottom Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.canPickThing()) { mark.pickThing(); mark.turnAround(); } else { mark.putThing(); mark.turnAround(); } RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.canPickThing()) { mark.pickThing(); } else { mark.putThing(); } mark.turnAround(); ITK 168

  19. Top Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.canPickThing()) { mark.turnAround(); mark.pickThing(); } else { mark.turnAround(); mark.move(); } RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); mark.turnAround(); if (mark.canPickThing()) { mark.pickThing(); } else { mark.move(); } ITK 168

  20. RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.countThingsInBackpack()>2) { mark.putThing(); mark.move(); } else { mark.putThing(); mark.turnArounde(); } Top Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); mark.putThing(); if (mark.countThingsInBackpack()>2) { mark.move(); } else { mark.turnAround(); } ITK 168

  21. Top Factoring RobotSE mark = new RobotSE(ny,0,0,....); ... if (mark.isFacingNorth()) { mark.turnAround(); mark.pickThing(); } else { mark.turnAround(); mark.move(); } RobotSE mark = new RobotSE(ny,0,0,....); ... mark.turnAround(); if (mark.isFacingNorth()) { mark.pickThing(); } else { mark.move(); } ITK 168

  22. Top Factoring int a=1,i=0; ... ... if (i==0) { i++; a = a+i; } else { i++; a = a*i; } // a == 2 int a=1,i=0; ... ... i++; if (i==0) { a = a+i; } else { a = a*i; } // a == 1 ITK 168

More Related