Presentation is loading. Please wait.

Presentation is loading. Please wait.

For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.

Similar presentations


Presentation on theme: "For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update."— Presentation transcript:

1 for Loops Programming

2 COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update

3 COMP102 Prog Fundamentals I: for Loops/Slide 3 The for Statement l Syntax for (initialization; condition; update) action l How it works: n execute initialization statement n while condition is true –execute action –execute update l Example: int i; for(i=1; i<=20; i++) cout << "i is " << i << endl;

4 COMP102 Prog Fundamentals I: for Loops/Slide 4 N! int number, factorial, n; cout > number; factorial = 1; for(n=1; n<=number; n++) factorial *= n; cout << "The factorial of " << number << " is " << factorial << endl;

5 COMP102 Prog Fundamentals I: for Loops/Slide 5 2N2N int number, result, n; cout > number; result = 1; for(n=1; n<=number; n++) result *= 2; cout << "Two raised to the " << number << " power is " << result << endl;

6 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;}

7 COMP102 Prog Fundamentals I: for Loops/Slide 7 Loops (Iteration) l Make sure there is a statement that will eventually stop the loop. à INFINITE LOOP == loop that never stops l 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. l Have a clear purpose for the loop.

8 COMP102 Prog Fundamentals I: for Loops/Slide 8 Increment and Decrement l C++ has special operators for incrementing (++) and decrementing (--) an integer by one. l The ++ operator functions as follows: n ++k : increments the value of k by one and the incremented value is used in the expression. n 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

9 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,

10 COMP102 Prog Fundamentals I: for Loops/Slide 10 Increment and Decrement The -- operator functions as follows: n --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

11 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

12 COMP102 Prog Fundamentals I: for Loops/Slide 12 Increment and Decrement l Examples: int k = 4; cout << k << endl; cout << ++k << endl; // k=k+1 : k is 5 cout << k++ << endl; // 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;

13 COMP102 Prog Fundamentals I: for Loops/Slide 13 Increment and Decrement l Examples: int k = 4; cout << k << endl; cout << --k << endl; // k=k-1 : k is 3 cout << k-- << endl; // 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;


Download ppt "For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update."

Similar presentations


Ads by Google