Do While Loops Indefinite Loops Do While Do While Vs

Do While Loops Indefinite Loops Do While Do While Vs

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-while loop – 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 infinite loop – Can be difficult to understand, so want to use sparingly (Appendix C) • 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 for loop 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 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    13 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us