Loop Statements Iteration Iteration o A portion of

  • Slides: 11
Download presentation
Loop Statements (Iteration)

Loop Statements (Iteration)

Iteration o A portion of a program that repeats a statement or group of

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

While Statements (Loops) o The while loop repeats as long as (while) the controlling

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

The do-while Statement o This statement loops like the while statement except the condition

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

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

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

Infinite Loops o Some care must be taken when writing loops to make sure

Infinite Loops o Some care must be taken when writing loops to make sure that they will eventually terminate. o 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. o In many systems if you hold down the control key and hit the c key, the program will end.

The for Statement o The for loop provides a convenient mechanism for looping when

The for Statement o The for loop provides a convenient mechanism for looping when it is known ahead of time how many times the loop should be executed. o 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);

Using the break Statement in Loops o The loop statement, while, do-while, and for,

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

An Example Using the break Statement for (item =1; item <= 10; item++) {

An Example Using the break Statement for (item =1; item <= 10; item++) { System. out. print("Enter cost of item #” + item + “: $"); amount = keyboard. next. Double(); total = total + amount; if (total >= 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);

The exit Method o If you want to exit the program and not just

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

Using the exit Method if (number. Of. Winners == 0) { System. out. println(“Error:

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