<<

do while Loops Indefinite Loops do while do while vs. while break continue Indefinite Loop Options

• Indefinite Loops – don’t know how many times will be executed – while loops are executed 0 or many times – May want to write loops that are executed at least once • do- – May want to write loops that are partially executed • break • continue

2 The do/while loop

◼ do/while loop: Executes statements repeatedly while a condition is true, testing it at the end of each repetition.

do { statement(s); } while (test);

◼ Example: // prompt until the user gets the // right password String phrase; do { System.out.print("Password: "); phrase = console.next(); } while (!phrase.equals("abracadabra"));

Copyright 2006 by Pearson Education do/while flow chart

◼ How does this differ from the while loop?

◼ The controlled statement(s) will always execute the first time, regardless of whether the test is true or false.

Copyright 2006 by Pearson Education Using a while loop Instead // prompt until the user gets the // right password String phrase; System.out.print("Password: "); phrase = console.next(); while (!phrase.equals("abracadabra"){ System.out.print("Password: "); phrase = console.next(); } // OR like this: String phrase = ""; while (!phrase.equals("abracadabra"){ System.out.print("Password: "); phrase = console.next(); } Usage of do-while loops • Need to do something at least once – Get a value from the user

• Don’t need to prime values – Controlled statements are guaranteed to execute at least once

• Remember to keep tested variables in scope – Declare outside of control structure

Note: Don’t use if executing the controlled statements at least once might cause an error.

6 Recall Dice class using while loop

// Rolls two dice until a sum of 7 is reached. import java.util.*; public class Dice { public static void main(String[] args) { Random rand = new Random(); int tries = 0; int sum = 0; //init to artificial value

while (sum != 7) { // roll the dice once int roll1 = rand.nextInt(6) + 1; int roll2 = rand.nextInt(6) + 1; sum = roll1 + roll2; System.out.println(roll1 + " + " + roll2 + " = " + sum); tries++; } System.out.println("You won after " + tries + " tries!"); } } Dice class using do/while // Rolls two dice until a sum of 7 is reached. import java.util.*; public class Dice { public static void main(String[] args) { Random rand = new Random(); int tries = 0; int sum;

do { int roll1 = rand.nextInt(6) + 1; // die 1 int roll2 = rand.nextInt(6) + 1; // die 2 sum = roll1 + roll2; System.out.println(roll1 + " + " + roll2 + " = " + sum); tries++; } while (sum != 7);

System.out.println("You won after " + tries + " tries!"); } } Breaks and Forever Loops • When you want a test in the middle of a seemingly – Can be difficult to understand, so want to use sparingly (Appendix )

• break; exits a loop – Only exits the loop containing the break • continue; exits current loop iteration

9 Example of breaks in while loop int sum = 0; while (true) { System.out.print("Next Int (-1 to quit)? "); while (!console.hasNextInt()) { console.next(); System.out.print("Not an integer. Try again: "); } int number = console.nextInt(); if (number == -1) { break; } sum += number; } System.out.println("sum = " + sum);

10 Example of continue in

int sum = 0; for (int i=0;i<100;i++) { System.out.print(" Type a nonnegative integer "); while (!console.hasNextInt()) { console.next(); System.out.print("Not an integer. Try again: "); } int number = console.nextInt(); if (number < 0) { continue; //skip this number } sum += number; } System.out.println("sum = " + sum);

11 switch Statement • Not tested on this, FYI only • Appendix C System.out.print("In what place did you finish? "); while (!console.hasNextInt()) { console.next(); System.out.print(“Please enter a whole number: "); } int place = console.nextInt(); switch (place) { case 1: System.out.println("You won the gold! "); break; case 2: System.out.println("You won the silver! "); break; case 3: System.out.println("You won the bronze! "); break; default: System.out.println("Sorry, no medal for you. "); break; }

12 In-class Exercise • Go to the moodle page and work on the PrintRandom.java assignment. • In the main method, – write a simple do/while loop that: • repeatedly prints random numbers between 0 and 1000 (inclusive) • until a number above 900 is printed. At least one line of output should always be printed. • The ouput for a sample execution of the program is shown below: csc$ java PrintRandom

Random Number: 242 Random Number: 19 Random Number: 805 Random Number: 139 Random Number: 962