3
Repeating an Action Calculating student grades with a loop read student name read number of correct answers of student calculate letter grade repeat until all students have been processed What do you need to determine when designing a loop? The actions the body of the loop will perform How/when will the loop stop

15
The for Statement All loops ◦…have an initialization/setup phase ◦…modify the condition that control when the looping ceases The for loop enables you to easily write a loop that clearly defines these steps ◦Such as loops controlled by a counter SYNTAX for (Initializing_Action; Boolean_Expression; Update_Action) Body Body can consist of multiple statements enclosed in braces { }

18
for and while Loops Initializing_Action; while (Boolean_Expression) { Statements... Update_Action; } for (Initializing_Action; Boolean_Expression; Update_Action;) Statements... } For loops are essentially a specialization of while loops

19
Multiple Statements in for Loops int i, j; for (i = 0, j = 0; i < 10; i++, j += 2) { System.out.println(i * j); } Initialization can include multiple statements separated by commas Same stands for the update statements What can you do for multiple conditions

21
Common Errors int product = 1, number; for (number = 1; number <= 10; number++); { product = product * number; } System.out.println("Product of the numbers 1 through " + "10 is " + product); A semicolon at the end means that the body of the for loop is empty

23
Picking the Right Loop Use a do-while loop only when you are certain that the loop needs to execute exactly once If your loop has well-defined initialization and update statements use a for loop Otherwise use while These are general guidelines, the final decision depends on the problem

24
for-each Statement Can be applied on variables that can only have certain values ◦Example: enumerations ◦We will later see other data that can be used with for-each loops enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}; for (Suit nextSuit : Suit.values()) System.out.print(nextSuit + " "); System.out.println();

25
Programming with Loops 1. Display instructions to the user 2. Initialize variables 3. Read a number into the variable next. 4. sum = sum + next 5. Display the number and the sum so far. 6. Read another number into the variable next. 7. sum = sum + next 8. Display the number and the sum so far. 9. Read another number into the variable next. 10. sum = sum + next 11. Display the number and the sum so far. 12. Read another number into the variable next. Write out the sequence of actions that you need to accomplish Identify patterns 1. Display instructions to the user. 2. Initialize variables. 3. Repeat the following for the appropriate number of times. { Read a number into the variable next. sum = sum + next Display the number and the sum so far. } Write the sequence using a loop

27
The break Statement A break statement within a loop causes an immediate exit from the loop body Use them wisely ◦To handle errors ◦If they actually make your code more readable ◦To handle rare conditions occurring Using break frequently makes loops harder to reason with In nested loops break only stops the innermost loop

29
The continue Statement A continue statement within a loop ends the current iteration and begins the next one Use them wisely ◦If they actually make your code more readable ◦To handle rare conditions occurring Using continue frequently makes loops harder to reason with In nested loops continue corresponds to the innermost loop In for loops it also triggers the update actions