This section uses the while repetition statement introduced in Chapter 4 to formalize the elements required to perform counter-controlled repetition, which requires
- a control variable (or loop counter)
需要control variable
- the initial value of the control variable
control value 的初始值
- the increment by which the control variable is modified each time through the loop (also known as each iteration of the loop)
the increment, x++
- the loop-continuation condition that determines if looping should continue.
還有一個 condition
To see these elements of counter-controlled repetition, consider the application of Fig. 5.1, which uses a loop to display the numbers from 1 through 10.
public class WhileCounter {
public static void main(String [] args) {
// declare and initialize control variable
int counter = 1;
// loop-continuation condition
while(counter <= 10){
System.out.printf("%d ", counter);
// increment control variable
++counter;
}
System.out.println();
}
}1 2 3 4 5 6 7 8 9 10
In Fig. 5.1, the elements of counter-controlled repetition are defined in lines 8, 10 and 13.
int counter = 1;
counter <= 10
++counter;Line 8 declares the control variable (counter) as an int, reserves space for it in memory and sets its initial value to 1.
int counter;Variable counter also could have been declared and initialized with the following local-variable declaration and assignment statements:
counter = 1;counter 變數可以分開宣告及賦值
Line 12 displays control variable counter’s value during each iteration of the loop.
System.out.printf("%d ", counter);把 counter 印出來
Line 13 increments the control variable by 1 for each iteration of the loop.
++counter; // increment control variable每次 loop, counter 加1
The loop-continuation condition in the while (line 10) tests whether the value of the control variable is less than or equal to 10 (the final value for which the condition is true).
while (counter <= 10) // loop-continuation condition當counter <= 10, while()裡面的值是 true
The program performs the body of this while even when the control variable is 10.
The loop terminates when the control variable exceeds 10 (i.e., counter becomes 11)
counter > 10, loop terminates
Because floating-point values may be approximate, controlling loops with floating-point variables may result in imprecise counter values and inaccurate termination tests.
因為 floating point 出來的數字已經不準了,再用 loop 去做四則運算更不準
The program in Fig. 5.1 can be made more concise by initializing counter to 0 in line 8 and preincrementing counter in the while condition as follows
while (++counter <= 10) // loop-continuation condition
System.out.printf("%d ", counter);可以把 ++counter 當成終止條件,放在結尾和放在 ( ) 裡面是一樣的
This code saves a statement, because the while condition performs the increment before testing the condition.
(Recall from Section 4.13 that the precedence of ++ is higher than that of <=.)
Coding in such a condensed fashion takes practice, might make code more difficult to read, debug, modify and maintain, and typically should be avoided.
這種 code 雖然行數比較少,但是能避免就避免
• Counter-controlled repetition (p. 153) requires a control variable, the initial value of the control variable, the increment by which the control variable is modified each time through the loop (also known as each iteration of the loop) and the loop-continuation condition that determines whether looping should continue.
couter-controlled repetition:
需要 control variable
the increment
loop continuation
• You can declare a variable and initialize it in the same statement.
可以宣告 variable 跟賦值寫在一起