0% found this document useful (0 votes)
44 views89 pages

Week3 1

The document discusses the C programming concepts of iteration and assignments. It introduces while, do-while and for loops to handle repetition in a program. It also describes compound assignment operators and pre/post-increment operators that can make code more concise. Examples are provided to illustrate computing factorials using a while loop and shorthand assignments.

Uploaded by

Rodrigo Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views89 pages

Week3 1

The document discusses the C programming concepts of iteration and assignments. It introduces while, do-while and for loops to handle repetition in a program. It also describes compound assignment operators and pre/post-increment operators that can make code more concise. Examples are provided to illustrate computing factorials using a while loop and shorthand assignments.

Uploaded by

Rodrigo Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

COMS11500: Introduction to C

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

This weeks problem


Ancient ASCII Art

Please enter the size of the canvas: 5 12


Please enter the horizontal line: 4
Please enter the vertical line: 4

The requested diagram is:

/--------------\
| \|/ |
| ---*-------- |
| /|\ |
| / | \ |
| / | \ |
\--------------/
Assignments

Outline

1 Problem

2 Assignments

3 Iteration
Basic Concepts
The while loop
The do...while loop
The for loop
Assignments

Assignment shortcuts

Compound assignment operators


Original C Code Corresponding compound
a = a + b; a += b;
a = a - b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;

int i=2, j=3; int i=2, j=3;


i = i + 2; i += 2;
j = i * j; j *= i;
j = j - 1; j -= 1;
i = i/(j-1); i /= j-1;
Assignments

Assignment shortcuts

Compound assignment operators


Original C Code Corresponding compound
a = a + b; a += b;
a = a - b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;

Post and pre-increment


Increasing i by 1 is even easier, instead of i+=1; write i++; or ++i;
i++; Elsewhere the old value of i is still used.
++i; The value of i is incremented prior to use elsewhere.
Similarly i- - and - -i are defined.
Assignments

Assignment shortcuts

int i=0; int i=0;


printf("i = %d\n", i++); printf("i = %d\n", ++i);
printf("i = %d\n",i); printf("i = %d\n",i);

i = 0 i = 1
i = 1 i = 1

Post and pre-increment


Increasing i by 1 is even easier, instead of i+=1; write i++; or ++i;
i++; Elsewhere the old value of i is still used.
++i; The value of i is incremented prior to use elsewhere.
Similarly i- - and - -i are defined.
Assignments

Assignment shortcuts

int i=1, j=3; int i=1, j=3;


j *= ++i; j *= i++;
printf("%d - %d", i, j); printf("%d - %d", i, j);

Post and pre-increment


Increasing i by 1 is even easier, instead of i+=1; write i++; or ++i;
i++; Elsewhere the old value of i is still used.
++i; The value of i is incremented prior to use elsewhere.
Similarly i- - and - -i are defined.
Assignments

Assignment shortcuts

int i=1, j=3; int i=1, j=3;


j *= ++i; j *= i++;
printf("%d - %d", i, j); printf("%d - %d", i, j);

2 - 6 2 - 3

Post and pre-increment


Increasing i by 1 is even easier, instead of i+=1; write i++; or ++i;
i++; Elsewhere the old value of i is still used.
++i; The value of i is incremented prior to use elsewhere.
Similarly i- - and - -i are defined.
Assignments

Assignment shortcuts

Compound assignment operators


Original C Code Corresponding compound
a = a + b; a += b;
a = a - b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;

Post and pre-increment


Increasing i by 1 is even easier, instead of i+=1; write i++; or ++i;
i++; Elsewhere the old value of i is still used.
++i; The value of i is incremented prior to use elsewhere.
Similarly i- - and - -i are defined.

Feel free to use or not use these shortcuts as you wish.


Iteration

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

The while construct

while (test) { statements }


Evaluates expression test
Performs statements iff (test)!=0 (true).
Evaluates expression test
...
Performs statements until test fails.
Iteration The while loop

The while construct

while (test) { statements }


Evaluates expression test
Performs statements iff (test)!=0 (true).
Evaluates expression test
...
Performs statements until test fails.

while (steps left?) {


take a step;
}
Iteration The while loop

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

The while flow


while (test) { statements }

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

Flow Diagram for do. . . while


do { statements } while (test);

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

The do. . . while construct


do { statements } while (test);
First performs statements once.
Evaluates expression test
Performs statements iff (test)!=0 (true).
Evaluates expression test
...
Performs statements until test fails.
Iteration The do...while loop

The do. . . while construct


do { statements } while (test);
First performs statements once.
Evaluates expression test
Performs statements iff (test)!=0 (true).
Evaluates expression test
...
Performs statements until test fails.

do {
take a step;
} while (steps left?);
Iteration The do...while loop

The do. . . while construct


do { statements } while (test);
First performs statements once.
Evaluates expression test
Performs statements iff (test)!=0 (true).
Evaluates expression test
...
Performs statements until test fails.

do {
take a step;
} while (steps left?);

Make sure you really do want to make at least one step!


Iteration The for loop

The for loop


for (init; test; update) { statements }
Semi-colons separate the three control parts
Any (or all) of the three control parts could be empty
The body is also allowed to be empty
If the test part is empty, it evaluates to true.
for(;;); is an infinite loop.
Iteration The for loop

The for loop


for (init; test; update) { statements }
Semi-colons separate the three control parts
Any (or all) of the three control parts could be empty
The body is also allowed to be empty
If the test part is empty, it evaluates to true.
for(;;); is an infinite loop.

for (i=0; i<#steps; i++) {


take a step;
}

for (i=1; i<=#steps; i++) {


take a step;
}
Iteration The for loop

The for loop


for (init; test; update) { statements }
Semi-colons separate the three control parts
Any (or all) of the three control parts could be empty
The body is also allowed to be empty
If the test part is empty, it evaluates to true.
for(;;); is an infinite loop.

for (i=0; i<#steps; i++) { for (i=#steps; i>0; i- -) {


take a step; take a step;
} }

for (i=1; i<=#steps; i++) { for (i=#steps-1; i>=0; i- -) {


take a step; take a step;
} }
Iteration The for loop

The flow of a for loop


for (init; test; update) { statements }

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

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
4 ? End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
4 1.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
4 1.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
4 4.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
4 4.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
3 4.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
3 12.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
3 12.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
2 12.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
2 24.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
2 24.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
1 24.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(4) test


true
n result false
1 24.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(0) test


true
n result false
0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(0) test


true
n result false
0 1.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(0) test


true
n result false
0 1.0 End
Iteration The for loop

The flow of a for loop


for (init; test; update) { 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 factorial(0) test


true
n result false
0 1.0 End

You might also like