1 / 14

Chapter 4: Loops and Files

Chapter 4: Loops and Files. Lecture 11: Wednesday October 11. Running Totals. Loops allow the program to keep running totals while evaluating data. Imagine needing to keep a running total of user input. Example: TotalSales.java. int days; // The number of days

jaugust
Download Presentation

Chapter 4: Loops and Files

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. Chapter 4: Loops and Files Lecture 11: Wednesday October 11

  2. Running Totals • Loops allow the program to keep running totals while evaluating data. • Imagine needing to keep a running total of user input.

  3. Example: TotalSales.java int days; // The number of days double sales; // A day's sales figure double totalSales; // Accumulator String input; // To hold the user's input // Create a DecimalFormat object to format output. DecimalFormat dollar = new DecimalFormat("#,##0.00"); // Get the number of days. input = JOptionPane.showInputDialog("For how many days " + "do you have sales figures?"); days = Integer.parseInt(input);

  4. Example: TotalSales.java totalSales = 0.0; for (int count = 1; count <= days; count++) { input = JOptionPane.showInputDialog("Enter the ”+“salesfor day " + count + ": "); sales = Double.parseDouble(input); totalSales += sales; // Add sales to totalSales. } JOptionPane.showMessageDialog(null, "The total sales are $" +dollar.format(totalSales));

  5. Sentinel Values • Sometimes (usually) the end point of input data is not known. • A sentinel value can be used to notify the program to stop acquiring input. • If it is a user input, the user could be prompted to input data that is not normally in the input data range (i.e. –1 where normal input would be positive.)

  6. Example: SoccerPoints.java int points; // Game points int totalPoints = 0; // Accumulator initialized to 0 // Display general instructions. System.out.println("Enter the number of points your team"); System.out.println( "has earned for each game this season."); System.out.println("Enter -1 when finished."); System.out.println(); // Get the first number of points. points = Integer.parseInt( JOptionPane.showInputDialog( "Enter game points or -1 to end: "));

  7. SoccerPoints.java // Accumulate the points until -1 is entered. while (points != -1) { totalPoints += points; points = Integer.parseInt( JOptionPane.showInputDialog( "Enter game points or -1 to end: ")); } // Display the total number of points. System.out.println("The total points are " + totalPoints);

  8. Nested Loops • Like if statements, loops can be nested. • If a loop is nested, the inner loop will execute all of its iterations for each time the outer loop executes once. for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) loop statements; • The loop statements in this example will execute 100 times.

  9. Example: Clock.java // Create a DecimalFormat object to format output. DecimalFormat fmt = new DecimalFormat("00"); // Simulate the clock. for (int hours = 1; hours <= 12; hours++) { for (int minutes = 0; minutes <= 59; minutes++) { for (int seconds = 0; seconds <= 59; seconds++) { System.out.print(fmt.format(hours) + ":"); System.out.print(fmt.format(minutes) + ":"); System.out.println(fmt.format(seconds)); } } }

  10. The break And continue Statements • The break statement can be used to abnormally terminate a loop. • The use of the break statement in loops bypasses the normal mechanisms and makes the code hard to read and maintain. • It is considered bad form to use the break statement in this manner.

  11. The continue Statement • The continue statement will cause the currently executing iteration of a loop to terminate and the next iteration will begin. • The continue statement will cause the evaluation of the condition in while and for loops. • Like the break statement, the continue statement should be avoided because it makes the code hard to read and debug.

  12. Deciding Which Loops to Use • The while loop: • Performs the test before entering the loop • Use it where you do not want the statements to execute if the condition is false in the beginning. • The do-while loop: • Performs the test after entering the loop • Use it where you want the statements to execute at least one time. • The for loop: • Performs the test before entering the loop • Use it where there is some type of counting variable that can be evaluated.

  13. Math.random() • Returns a randomvalue between 0 and 1. • Can be used to generate a number between x and y: • double r = Math.random()*(y-x) +x;

  14. Math.round() • Rounds doubles to the nearest integer. • Example: • Math.round(Math.random()*(y-x))+x; • Returns a long value between x and y.

More Related