Download presentation
Presentation is loading. Please wait.
Published byJudith Horton Modified over 9 years ago
1
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java provides three forms for explicit loops:Java provides three forms for explicit loops: –while –for –do..while The conditions in loops are written as in if statementsThe conditions in loops are written as in if statements The break statement breaks out of the loop that it is withinThe break statement breaks out of the loop that it is within See examples on next slidesSee examples on next slides while (condition) { statement; … statement; } for (start; condition; step) { statement; … statement; } do { statement; … statement; } while (condition);
2
Fundamentals of Software Development 1Slide 2 for-loop example Problem: Display the sine of 0.01, sine of 0.02, … sine of 4.00.Problem: Display the sine of 0.01, sine of 0.02, … sine of 4.00. Solution idea: Use a counting variableSolution 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 …? 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)); } Both solutions are correct, although the first risks roundoff error in its stopping condition
3
Fundamentals of Software Development 1Slide 3 while-loop example Problem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative numberProblem: 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: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 yourselfExercise: 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
Fundamentals of Software Development 1Slide 4 Problem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative numberProblem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative number while (true) { input = inputStream.nextDouble(); if (input < 0) { break; } System.out.println(Math.sin(input)); } 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. Scanner inputStream = new Scanner(System.in); double input; input = inputStream.nextDouble(); while (input >= 0) { System.out.println(Math.sin(input); input = inputStream.nextDouble(); } This first, then either of the next two boxes.
5
Fundamentals of Software Development 1Slide 5 for loops versus while loops Typically we use: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 for (int i = 0; i < 7; i = i + 1) { System.out.println (i + " " + i*i); } int i = 0; while (i < 7) { System.out.println (i + " " + i*i); i = i + 1; } The two boxes are equivalent.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.