Best web hosting - 202 Control Structures: Part 2 Chapter 5 The

202 Control Structures: Part 2 Chapter 5 The applet s paint method operates as follows: When the for structure (lines 20 21) begins executing, the control variable counteris initialized to 1. (The first two elements of counter-controlled repetition and the name of the control variable and its initial value.) Next, the program checks the loop-continuation condition, counter<=10. The condition contains the final value (10) of the control variable. Because the initial value of counteris 1, the condition is satisfied (true), so the body statement (line 21) draws a line. After executing the body of the loop, the program increments variable counter in the expression counter++. Then, the program performs the loop-continuation test again to determine whether the program should continue with the next iteration of the loop or whether it should terminate the loop. At this point, the control variable value is 2, so the condition is true (i.e., the final value is not exceeded), and thus the program performs the body statement again (i.e., the next iteration of the loop). This process continues until the counter s value becomes 11, causing the loop-continuation test to fail and repetition to terminate. Then, the program performs the first statement after the for structure. (In this case, method paintterminates, because the program reaches the end of paint.) Notice that Fig. 5.2 uses the loop-continuation condition counter<=10. If the programmer incorrectly specified counter < 10 as the condition, the loop would be executed only nine times. This mistake is a common logic error called an off-by-one error. Common Programming Error 5.2 Using an incorrect relational operator or using an incorrect final value of a loop counter in the condition of a while, for or do/while structure can cause an off-by-one error. Good Programming Practice 5.6 Using the final value in the condition of a while or for structure and using the <= relational operator will help avoid off-by-one errors. For a loop that prints the values 1 to 10, the loop-continuation condition should be counter <= 10 rather than counter < 10 (which causes an off-by-one error) or counter < 11 (which is correct). Many programmers prefer so-called zero-based counting, in which to count 10 times, counter would be initialized to zero and the loop-continuation test would be counter < 10. Figure 5.3 takes a closer look at the forstructure of Fig. 5.2. The forstructure s first line (including the keyword for and everything in parentheses after for) is sometimes called the for structure header. Notice that the for structure does it all : It specifies each of the items needed for counter-controlled repetition with a control variable. If there is more than one statement in the body of the forstructures, braces ({and }) are required to define the body of the loop. forkeyword Control variable name Final value of control variable for ( int counter = 1; counter <= 10; counter++ ) Initial value of control variable Increment of control variable Loop-continuation condition Fig. 5.3Components of a typical forstructure header. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply