Presentation is loading. Please wait.

Presentation is loading. Please wait.

LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.

Similar presentations


Presentation on theme: "LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds."— Presentation transcript:

1 LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds of loops: the while loop, the do-while loop, and the for loop. 1 1

2 THE while LOOP The while loop/statement has the form:
while (Boolean expression) { statement(s); } The while(Boolean expression) is the loop header. It consists of the key word while and a parenthesized Boolean expression, often called the loop continuation expression. The body of the loop follows the header. The body of the loop is the statement or block of statements that are to be repeated. 2 2

3 THE while LOOP while (Boolean expression) { statement(s); } When execution reaches a while statement, the parenthesized loop continuation expression is evaluated, if the expression is true the statement(s) that make up the body are executed and then execution returns to the evaluation of the continuation expression. This process is repeated until the loop continuation expression is evaluated and has the value false. When this happens, execution continues with the statement following the while statement. 3 3

4 THE while LOOP while (Boolean expression) { statement(s); } One repetition of the body of a loop is called an iteration. The while loop is a pretest loop. The continuation expression is tested before the body of the loop is executed. If the continuation expression is false on the initial test, the body of the loop is never executed, i.e. it never iterates. 4 4

5 THE while LOOP Flowchart Representation of a while Loop
5 5

6 THE while LOOP The loop continuation expression must eventually become false or an infinite loop results. An infinite loop is a loop that does not have a way to terminate normally. 6 6

7 THE while LOOP Don’t put a semicolon here. The loop header is only the beginning of the while statement. while (Boolean expression) { statement(s); } The braces are not necessary if the body of the loop is a single statement. Notice that there is no semicolon after the loop continuation expression. A complete while statement includes a loop header and the loop body. 7 7

8 THE while LOOP while (Boolean expression) { statement(s); } It is considered good programming style to begin the body of the loop on the line below the continuation expression and indent the statement(s) that make up the body of the loop one level from the key word while. 8 8

9 THE while LOOP /** This program demonstrates the while loop. */
public class WhileLoop { public static void main(String [ ] args) int number = 1; while (number <= 5) System.out.println("Hello"); number++; } System.out.println("That's all!"); 9 9

10 THE while LOOP The loop in WhileLoop.java is a particular kind of loop known as a count-controlled loop. A count-controlled loop is a loop that iterates a specific number of times. This type of loop uses a control variable, usually called a counter, to control the number of times the loop iterates. When you construct a count-controlled loop you must be sure to: 1. Give the counter a starting value. 2. Compare the counter to a maximum (or minimum) value in the loop continuation expression. When the counter reaches this value the loop terminates. 3. Update the value of counter. 10 10

11 THE while LOOP In the program WhileLoop.java the variable named number is the counter used to control the number of iterations of the loop. /** Code Listing 4-3 from the text. This program demonstrates the while loop. */ public class WhileLoop { public static void main(String [ ] args) int number = 1; while (number <= 5) System.out.println("Hello"); number++; } System.out.println("That's all!"); 1.We are giving the counter, named number, a starting value, count, of 1. 2. We are comparing the value in the counter to the upperbound on the count, in this case it is 5. 3. Each iteration of the loop we are updating the value in the counter with this statement. 11 11

12 USING THE while LOOP FOR INPUT VALIDATION
According to the good programming style, "input validation is the process of inspecting data given to a program by the user and determining if it is valid. A good program should give clear instructions about the kind of input that is acceptable, and not assume the user has followed these instructions." When the user enters invalid data the program should: 1. Display a descriptive error message 2. Ask for another input 3. Read the user's new input An input validation loop iterates until the user enters valid data. 12 12

13 USING THE while LOOP FOR INPUT VALIDATION
The logic for the while loop version of an input validation loop is shown here. 13 13

14 THE do-while LOOP The do-while loop/statement has the form:
{ statement(s); } while (Boolean expression); The key word do is followed by the body of the loop, the key word while, the parenthesized loop continuation expression, and a semicolon. 14 14

15 THE do-while LOOP do { statement(s); } while (Boolean expression); When execution reaches a do-while statement, the statement or block of statements that make up the body of the loop are executed, and then the loop continuation expression is evaluated. If this expression evaluates to true, execution continues at the first statement of the loop body. This process is repeated until the loop continuation expression is evaluated and has the value false. When the continuation expression evaluates to false, execution continues with the first statement following the do-while statement. 15 15

16 THE do-while LOOP do { statement(s); } while (Boolean expression); The do-while loop is a posttest loop. The loop continuation expression is evaluated at the end of an iteration of the loop body. The do-while loop iterates at least once. 16 16

17 THE do-while LOOP Flowchart Representation of a do-while Loop
17 17

18 THE do-while LOOP do { statement(s); } while (Boolean expression); There must be a semicolon after the loop continuation expression of a do-while statement. The braces are not necessary if the body of the loop is a single statement. Notice there is a semicolon here, because this is the end of the do-while statement. 18 18

19 THE do-while LOOP do { statement(s); } while (Boolean expression); It is considered good programming style to begin the body of the loop on the line below the key word do and indent the statements that make up the loop body one level from the word do. 19 19

20 THE do-while LOOP The do-while loop is ideal for situations where we want the body of the loop to iterate at least once. 20 20

21 THE for LOOP The for loop/statement has the form:
for (initialization; Boolean expression; update) { statement(s); } The for(initialization; Boolean expression; update) is the loop header. It consists of the key word for followed by three parenthesized expressions separated by semicolons. Notice there is not a semicolon after the third expression, the update. 21 21

22 THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The first part is the initialization. It is typically used to initialize a counter, accumulator, or other variable that must be given a starting value. The initialization is only executed before the first iteration of the loop. 22 22

23 THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The second expression is the loop continuation expression. This is a Boolean expression that gets evaluated before every iteration of the loop to determine if the loop should iterate. 23 23

24 THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The third expression is the update. The update is executed at the end of each iteration before execution returns to the continuation expression. Typically, this is a statement that updates the value of a variable that is used to control the loop. 24 24

25 This is the body of the loop.
THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The body of the loop follows the header. The body of the loop is the statement or block of statements that are to be repeated. This is the body of the loop. 25 25

26 THE for LOOP for (initialization; Boolean expression; update) { statement(s); } When execution reaches a for statement, the initialization is executed. The loop continuation expression is evaluated, if this expression is true, the statement(s) that make up the body of the loop are executed and then the update is executed before returning to the evaluation of the loop continuation expression. The process described in the previous bullet is repeated until the loop continuation expression is evaluated and has the value false. When this occurs, execution continues with the statement following the for statement. 26 26

27 THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The for loop is a pretest loop. If the continuation expression evaluates to false on the initial test, the body of the loop does not iterate (the statements in the loop body and the update are never executed). 27 27

28 THE for LOOP Flowchart Representation of a for Loop
28 28

29 THE for LOOP Don’t put a semicolon here. The loop header is only the beginning of the for statement. for (initialization; Boolean expression; update) { statement(s); } Notice that there is no semicolon after the loop header. A complete for loop/statement includes the loop body. The braces are not necessary if the body of the loop is a single statement. 29 29

30 THE for LOOP for (initialization; Boolean expression; update) { statement(s); } It is considered good programming style to begin the body of the loop on the line below the parenthesized expressions and indent the statements that make up the body of the loop one level from the key word for. 30 30

31 THE for LOOP Problem: Write the pseudocode for a program that gets 50 grades from the user and calculates and displays the average of the grades on the computer screen. 31 31

32 THE for LOOP An accumulator is a variable that is used to store a running total that “accumulates” over the iterations of the loop. When you use an accumulator you must be sure to give it a starting value before the first iteration of the loop. 32 32


Download ppt "LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds."

Similar presentations


Ads by Google