A. Introduction
The third and last type of flow is all about repetitions: how to repeat some actions exact N times? or how to repeat some actions until certain status is achieved? There are actually three types of statements: for loops, while loops and do loops.
B. Watch(Slides)
C. Try:
R6.3
a. 1,3,6,10,15,21,28.36
b. 0,2,1,3,2,4,3,5,4,6
c. 0 0 0 0 0 0 0 0 0 0 0 0 0 0....
d. 1 2 8 64
r3.a 1, 3, 6, 10, 15, 21, 28, 36, 45, 55
r3.b 0, 2, 1, 4, 3, 5, 4, 6, 5, 7
r3.c 5, 20, 60, 120, 120
r3.d 1, 2, 8, 64
r7.a 1 2 3 4 5 6 7 8 9
r7.b 1 3 5 7 9
r7.c 10 9 8 7 6 5 4 3 2
r7.d 0 1 2 3 4 5 6 7 8 9
r7.e 1 2 4 8
r7.f 2 4 6 8
r21.a 2 4 7 11 16
r21.b 4 9 16
r21.c 10 7
R6.3 a. 1 b. 0 c. 5 d. 2
R6.5 ?
R6.7 a. 9 b. error c. 2 d. 9 e. 8 f. error
R6.21 a. 6 b. 1 c. 6 5
Some hints on R6.5:
/* The code below returns sum of 0-4 */
int sum =0; // step 1: define a variable to save the sum
for(int i=0; i<5; i++){ // step 2: using for loop to add one number at a time
sum = sum + i;
}
System.out.printf("The sum of 0 to 4 is: %d \n\n\n", sum); // step 3: print out the sum
/* The code below returns a digit of a number */
int n = 978645312; // original number
int m = n; // make a copy for output
for(int j=8; j>=0; j--){
int digit = n/(int)Math.pow(10,j); // Highest digit is (int)n/10^8
n= n-digit*(int)Math.pow(10,j); // reduce n to get next highest digit
System.out.printf("The 10^%d digit of %d is: %d\n", j, m, digit); // print it out to verify
}