Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.

Similar presentations


Presentation on theme: "Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of."— Presentation transcript:

1 Loop Statements (Iteration)

2 Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of the loop body is called an iteration of the loop.  In addition to the loop mechanism we need a way for deciding when to end the loop (stop iterating).

3 While Statements (Loops)  The while loop repeats as long as (while) the controlling condition is true.  If the condition is false immediately, the loop is skipped.  Example: while (count <= number) { System.out.print(count + “, “); count++; }

4 The do-while Statement  This statement loops like the while statement except the condition is not tested until the end of the loop.  Unlike the while statement, the statement or block of statements in the loop are always executed at least once.

5 A do-while Example do { System.out.print(count + “, “); count++; }while (count <= number);

6 Infinite Loops  Some care must be taken when writing loops to make sure that they will eventually terminate.  If the condition you are testing for ending the looping never becomes true, e.g., ( 1 == 2 ), the loop will run until the program is terminated by some external means.  In many systems if you hold down the control key and hit the c key, the program will end.

7 The for Statement  The for loop provides a convenient mechanism for looping when it is known ahead of time how many times the loop should be executed.  For example, if we wanted to print out the first seven integers, we could write: for (count = 1; count <= 7; count++) System.out.println(count);

8 Using the break Statement in Loops  The loop statement, while, do-while, and for, as previously described always complete their entire loop body on each iteration.  Sometimes, however, you may want to end the loop in the middle of the loop body.  The break statement allows you to do this by transferring control to the first statement following the loop.

9 An Example Using the break Statement for (item =1; item = 100) { System.out.println("You spent all your money."); break; } System.out.println("Your total so far is $" + total); System.out.println("You may purchase up to " + (10 - item) + " more items."); } System.out.println("You spent $" + total);

10 The exit Method  If you want to exit the program and not just exit a loop, you can use the exit method.  If the statement, System.exit(0) is executed, the Java program will be immediately terminated.

11 Using the exit Method if (numberOfWinners == 0) { System.out.println(“Error: Dividing by zero.”); System.exit(0); } else { oneShare = payoff/numberOfWinners; System.out.println(“Each winner will receive $” + oneShare); }


Download ppt "Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of."

Similar presentations


Ads by Google