Cheap web hosting - Chapter 5 Control Structures: Part 2 203 The
Wednesday, October 24th, 2007Chapter 5 Control Structures: Part 2 203 The general format of the for structure is for ( expression1; expression2; expression3 ) statement where expression1 names the loop s control variable and provides its initial value, expression2 is the loop-continuation condition (containing the control variable s final value) and expression3 modifies the value of the control variable, so that the loop-continuation condition eventually becomes false. In most cases, the for structure can be represented with an equivalent while structure, with expression1, expression2 and expression3 placed as follows: expression1; while ( expression2 ) { statement expression3; } In Section 5.7, we show a case in which a for structure cannot be represented with an equivalent while structure. If expression1 (the initialization section) declares the control variable inside the parentheses of the header of the forstructure (i.e., the control variable s type is specified before the name of the variable), the control variable can be used only in the for structure. This restricted use of the name of the control variable is known as the variable s scope. The scope of a variable defines where the program can use the variable. For example, we mentioned previously that a program can use a local variable only in the method that declares the variable. Scope is discussed in detail in Chapter 6, Methods. Common Programming Error 5.3 When the control variable of a for structure is initially defined in the initialization section of the header of the for structure, using the control variable after the body of the structure is a syntax error. Sometimes, expression1 and expression3 in a for structure are comma-separated lists of expressions that enable the programmer to use multiple initialization expressions and/or multiple increment expressions. For example, there may be several control variables in a single for structure that must be initialized and incremented. Good Programming Practice 5.7 Place only expressions involving the control variables in the initialization and increment sections of a for structure. Manipulations of other variables should appear either before the loop (if they execute only once, like initialization statements) or in the body of the loop (if they execute once per iteration of the loop, like incrementing or decrementing statements). The three expressions in the for structure are optional. If expression2 is omitted, Java assumes that the loop-continuation condition is true, thus creating an infinite loop. One might omit expression1 if the program initializes the control variable before the loop. One might omit expression3 if the program calculates the increment with statements in the loop s body or if the loop does not require an increment. The increment expression in the for structure acts as a stand-alone statement at the end of the body of the for structure, so the expressions Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01