Week3 1
Week3 1
Dimitrios V. Vavoulis
15 Oct, 2013
Problem
Outline
1 Problem
2 Assignments
3 Iteration
Basic Concepts
The while loop
The do...while loop
The for loop
Problem
/--------------\
| \|/ |
| ---*-------- |
| /|\ |
| / | \ |
| / | \ |
\--------------/
Assignments
Outline
1 Problem
2 Assignments
3 Iteration
Basic Concepts
The while loop
The do...while loop
The for loop
Assignments
Assignment shortcuts
Assignment shortcuts
Assignment shortcuts
i = 0 i = 1
i = 1 i = 1
Assignment shortcuts
Assignment shortcuts
2 - 6 2 - 3
Assignment shortcuts
Outline
1 Problem
2 Assignments
3 Iteration
Basic Concepts
The while loop
The do...while loop
The for loop
Iteration Basic Concepts
Climbing stairs
Suppose you are at the bottom of a stair...
...you want to go up....
What to do?
Iteration Basic Concepts
Climbing stairs
Suppose you are at the bottom of a stair...
...you want to go up....
What to do?
Take a step;
Take a step;
Take a step;
Take a step;
Take a step;
Take a step;
...
Take a step;
Take a step;
Take a step;
Take a step;
Iteration Basic Concepts
Climbing stairs
Suppose you are at the bottom of a stair...
...you want to go up....
What to do?
Take a step;
Take a step;
Better ways to deal with repetition
Take a step;
Take a step; While youre not at the top, take a step;
Take a step;
Take a step; Take a step until you reach the top;
...
Take a step; If there are x steps, repeat Take a step x
Take a step; times.
Take a step;
Take a step;
Iteration Basic Concepts
Climbing stairs
Suppose you are at the bottom of a stair...
...you want to go up....
What to do?
Take a step;
Take a step;
Better ways to deal with repetition
Take a step;
Take a step; While youre not at the top, take a step;
Take a step; roughly corresponds with Cs while loop
Take a step; Take a step until you reach the top;
... is similar to Cs do. . . while loop
Take a step; If there are x steps, repeat Take a step x
Take a step; times.
Take a step; best captured by Cs for loop
Take a step;
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
4 ?
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
4 1.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
4 1.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
4 4.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
3 4.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
3 4.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
3 4.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
3 12.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
2 12.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
2 12.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
2 12.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
2 24.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
1 24.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
1 24.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(4)
n result
1 24.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(0)
n result
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(0)
n result
0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(0)
n result
0 1.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(0)
n result
0 1.0
Iteration The while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
while (n>1) {
result *= (double) n;
n- -; statements
}
return result;
test
} true
false
End
Computing factorial(0)
n result
0 1.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
4 ?
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
4 1.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
4 1.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
4 4.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
3 4.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
3 4.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
3 4.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
3 12.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
2 12.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
2 12.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
2 12.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
2 24.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
1 24.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
1 24.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(4)
n result
1 24.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(0)
n result
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(0)
n result
0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(0)
n result
0 1.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(0)
n result
0 1.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(0)
n result
0 0.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(0)
n result
-1 0.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(0)
n result
-1 0.0
Iteration The do...while loop
Computing n!
double factorial(int n) {
double result=1.0; Start
do {
result *= (double) n;
n- -; statements
} while (n>1);
return result;
test
} true
false
End
Computing factorial(0)
n result
-1 0.0
Iteration The do...while loop
do {
take a step;
} while (steps left?);
Iteration The do...while loop
do {
take a step;
} while (steps left?);
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
test
true
false
End
Iteration The for loop
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}
Computing n! Start
double factorial(int n) {
init
double result=1.0;
for (; n>1; n- -) {
result *= (double) n;
update
}
return result;
statements
}