1 / 17

Iteration Structures

Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration control structures test a condition each time through the loop. Types of Loops. There are three types of loops that Java employs.

Download Presentation

Iteration Structures

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. Iteration Structures • Iteration is the third control structure we will explore. • Iteration simply means to do something repeatedly. • All iteration control structures test a condition each time through the loop.

  2. Types of Loops • There are three types of loops that Java employs. • Pretest Loop- test a condition each time before the loop is executed. • Posttest Loop- test a condition after each loop execution. • Fixed repetition- cause a loop to be executed a predetermined number of times.

  3. Three Iteration Control Structures • The three iteration control structures are the while, do/while, and for. • The difference between them is the means by which they control the exiting of the loop. • The while is a pretest loop. • The do/while is a posttest loop. • The for is a fixed repetition loop.

  4. The WHILE loop • If the test expression is true the loop statements are executed. • If the test expression is false the loop statements are bypassed. • To get out of the loop something must change the expression to false, otherwise the result will be an infinite loop.

  5. Syntax for the While Loop while (test expression) { statement1; statement2; statementn; }

  6. The DO/WHILE loop • The do/while loop is tested at the end of the loop compared to the while which is tested at the beginning. • The loop statements will always be executed at least once. • To break the loop the test expression must become false, otherwise, just like the while loop, the result will be an infinite loop.

  7. Syntax for DO/WHILE do { statement1; statement2; statementn; } while (test expression);

  8. Using Counters and Accumulators • Numeric variables used within a repetition structure to calculate subtotals, totals, averages and run the loop. • Counter • Used for counting at constant rate. • Ex. counter += 1 or counter++ • Accumulator • Used for accumulating (adding together) changing values. • Ex. accumulator += value

  9. Sentinel Values • Values used to end loops • Should be easily distinguishable from the valid data used by the program • Also called trip values or trailer values • Should either be declared as a constant variable or regular variable and ask the user to enter a value.

  10. The FOR loop • The for loop runs a fixed number of times • The first thing that is done is the initialization of the counter. • If the results of the test expression is true, then the statements are executed. • Each time the loop is executed, the loop counter must be incremented or decremented.

  11. Syntax for FOR loop for (initialize counter; test counter; update counter) { statement1; statement2; statementn; }

  12. Nested Loops • Many times it is desirable to have looping operations within loops. • This is called nested looping. • Each inner loop will run as many times as desired X the number of times the outer loop runs.

  13. Break and Continue Option • If you want a loop to terminate when a desired value is found, it is possible to use the break statement to force the loop to quit. • If you want one iteration of the loop to be terminated, it is possible to use a continue statement to force the loop to quit one iteration and continue on with another.

  14. Syntax for BREAK while (test expression) { statement1; statement2; if (test expression) break; statementn; }

  15. Syntax for CONTINUE for (initialize counter; test counter; update counter) { statement1; statement2; if (test expression) continue; statementn; }

  16. Number Formatting • Import • import java.text.DecimalFormat; • Instantiation • DecimalFormat twoDecimal = new DecimalFormat("0.00"); • The numbers inside of parenthesis are the way in which you want your numbers to show up when you output. • Ouput • twoDecimal.format(variable);

  17. Example Formatting Program • import TerminalIO.KeyboardReader; • import java.text.DecimalFormat; • public class FormatExample • { • public static void main(String [] args) • { • KeyboardReader reader = new KeyboardReader(); • DecimalFormat twoDecimal = new DecimalFormat("0.00"); • DecimalFormat oneDecimal = new DecimalFormat("0.0"); • double number; • number = reader.readDouble("Please enter a number: "); • System.out.println("Two decimal format: " + twoDecimal.format(number)); • System.out.println("One decimal format: " + oneDecimal.format(number)); • } • }

More Related