Checklist for repetition Control 1. Watch out for the off-by-one error(OBOe) 2. Make sure the loop body contains a statement that will eventually cause the loop to terminate 3. Make sure the loop repeats exactly the correct number of times 4. If you want to execute the loop body N times, then initialize the counter to o and use the test condition counter n or initialize the counter to 1 and use the test condition counter < N C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7-11
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 11 Checklist for Repetition Control 1. Watch out for the off-by-one error (OBOE). 2. Make sure the loop body contains a statement that will eventually cause the loop to terminate. 3. Make sure the loop repeats exactly the correct number of times. 4. If you want to execute the loop body N times, then initialize the counter to 0 and use the test condition counter < N or initialize the counter to 1 and use the test condition counter <= N
Useful Shorthand Operators sum sum t numberi is equivalent to sum + number Operator Usage Meaning a+=b; a a t bi a a a b: 大 b a/=b a a/ b 8=b ao bi C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-12
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 12 Useful Shorthand Operators sum = sum + number; is equivalent to sum += number; Operator Usage Meaning += a += b; a = a + b; -= a -= b; a = a – b; *= a *= b; a = a * b; /= a /= b; a = a / b; %= a %= b; a = a % b;