Transcript for
Programming
for Loops
COMP102 Prog Fundamentals I: for Loops/Slide 2
The for Statement
initialization
condition
true
action
update
false
COMP102 Prog Fundamentals I: for Loops/Slide 3
The for Statement
Syntax
for (initialization; condition; update)
action
How it works:
execute initialization statement
while condition is true
– execute action
– execute update
Example:
int i;
for(i=1; i<=20; i++)
cout << "i is " << i << endl;
COMP102 Prog Fundamentals I: for Loops/Slide 4
N!
int number, factorial, n;
cout << "Enter positive integer:";
cin >> number;
factorial = 1;
for(n=1; n<=number; n++)
factorial *= n;
cout << "The factorial of " << number
<< " is " << factorial << endl;
COMP102 Prog Fundamentals I: for Loops/Slide 5
2N
int number, result, n;
cout << "Enter positive integer:";
cin >> number;
result = 1;
for(n=1; n<=number; n++)
result *= 2;
cout << "Two raised to the "
<< number << " power is "
<< result << endl;
int main() {
int listSize, n;
int value;
double sum = 0;
double average;
cout << "How many input numbers? " << endl;
cin >> listSize;
cout << " Please enter the " << listSize
<< " input numbers: " << endl;
for (n=listSize; n > 0; n--) {
cin >> value;
sum += value;
}
if (listSize >0) {
average = sum / listSize;
cout << "Average: " << average << endl;
}
return 0;}
COMP102 Prog Fundamentals I: for Loops/Slide 7
Loops (Iteration)
Make sure there is a statement that will
eventually stop the loop.
INFINITE
LOOP == loop that never stops
Make sure to initialize loop counters correctly.
OFF-BY-ONE
== the number of times that a loop is
executed is one too many or one too few.
Have a clear purpose for the loop.
COMP102 Prog Fundamentals I: for Loops/Slide 8
Increment and Decrement
C++ has special operators for incrementing (++) and
decrementing (--) an integer by one.
The ++ operator functions as follows:
++k: increments the value of k by one and the incremented
value is used in the expression.
Example:
int k = 1, j;
j =++k;//First: k increased by 1, becomes 2,
// Second: j is assigned to 2
k = 3;
cout << ++k;
// First: k is increased by 1, becomes 4,
// Second: 4 outputs to the screen
COMP102 Prog Fundamentals I: for Loops/Slide 9
Increment and Decrement
k++ uses the initial value of k in the expression
and increments afterwards.
Example:
int k = 1, j;
j = k++; // First: j is assigned to 1
//Second: k is increased by 1, becomes 2
k = 3;
cout << k++;
//First:3 printed on the screen
// Second:k is increased by 1, becomes 4,
COMP102 Prog Fundamentals I: for Loops/Slide 10
Increment and Decrement
The -- operator functions as follows:
--k decrements the value of k by one and the
decremented value is used in the expression.
Example:
int k = 1, j;
j =--k; // First: k decreased by 1, become 0,
// Second: j is assigned to 0
k = 3;
cout << --k;
// First: k is decreased by 1, becomes 2,
// Second: 2 is output to the screen
COMP102 Prog Fundamentals I: for Loops/Slide 11
Increment and Decrement
k-- uses the initial value of k in the expression and
decrements afterwards.
Example:
int k = 1, j;
j = k--; // first: j is assigned to 1
// second: k decreased by 1, becomes 0,
k = 3;
cout << k--;
//First: 3 output to the screen
//Second: k is decreased by 1, becomes 2
COMP102 Prog Fundamentals I: for Loops/Slide 12
Increment and Decrement
Examples:
int k = 4;
cout << k << endl;
cout << ++k << endl;
cout << k++ << endl;
// k=k+1 : k is 5
// k=k+1 : k is 6
int i = k++;
// i is 6, k is 7
cout << i << " " << k << endl;
int j = ++k;
// j is 8, k is 8
cout << j << " " << k << endl;
COMP102 Prog Fundamentals I: for Loops/Slide 13
Increment and Decrement
Examples:
int k = 4;
cout << k << endl;
cout << --k << endl;
cout << k-- << endl;
// k=k-1 : k is 3
// k=k-1 : k is 2
int i = k--;
// i is 2, k is 1
cout << i << " " << k << endl;
int j = --k;
// j is 0, k is 0
cout << j << " " << k << endl;