1 / 5

Loops

Loops. while ( condition ) { statement; … statement; }. A loop is: a block of code that executes repeatedly while some condition holds true. Java provides three forms for explicit loops: while for do..while The conditions in loops are written as in if statements

nathanielr
Download Presentation

Loops

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. Loops while (condition) { statement; … statement; } • A loop is: • a block of code that executes repeatedly while some condition holds true. • Java provides three forms for explicit loops: • while • for • do..while • The conditions in loops are written as in if statements • The break statement breaks out of the loop that it is within • See examples on next slides for (start; condition; step) { statement; … statement; } do { statement; … statement; } while (condition);

  2. for-loop example • Problem: Display the sine of 0.01, sine of 0.02, … sine of 4.00. • Solution idea: Use a counting variable • What should the variable start at? • The loop should continue while …? • Each time through the loop, increment the variable by …? Both solutions are correct, although the first risks roundoff error in its stopping condition for (double x = 0.01; x <= 4.00; x = x + 0.01) { System.out.println(Math.sin(x)); } for (int k = 1; k <= 400; ++k) { System.out.println(Math.sin(k/100.0)); }

  3. while-loop example • Problem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative number • Three different approaches to solving this problem: • Use a while loop that runs forever, but break when a negative number is input • First do an input, then use a while loop that runs while inputs are nonnegative • As above, but using a for loop • Exercise: Work through the first solution approach yourself • Don’t get hung up on the notation for getting input • Solutions are on the next slide

  4. This first, then either of the next two boxes. Scanner inputStream = new Scanner(System.in); double input; • Problem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative number input = inputStream.nextDouble(); while (input >= 0) { System.out.println(Math.sin(input); input = inputStream.nextDouble(); } Both solutions are correct. The first emphasizes the stopping condition, at the expense of an awkward start (repeating the same statement before and within the loop). The first solution could be converted to a for-loop easily. while (true) { input = inputStream.nextDouble(); if (input < 0) { break; } System.out.println(Math.sin(input)); }

  5. for loops versus while loops for (int i = 0; i < 7; i = i + 1) { System.out.println (i + " " + i*i);} • Typically we use: • for when we know in advance how many times the loop will execute • while when something that happens in the loop determines when the loop exits • do..while when we want a while loop that always does at least one iteration int i = 0; while (i < 7) { System.out.println (i + " " + i*i); i = i + 1; } The two boxes are equivalent.

More Related