130 likes | 252 Views
This guide provides a comprehensive overview of the for loop in C++, focusing on its syntax, initialization, and execution flow. Learn how to define conditions, update counters, and execute actions within the loop. Explore practical examples including calculating factorials and raising two to a power, along with essential tips to avoid common pitfalls like infinite loops and off-by-one errors. Gain insights into increment and decrement operations using special operators (++ and --) in this essential programming concept.
E N D
Programming for Loops
The for Statement initialization condition false true action update
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;
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;
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;}
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.
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 2k = 3;cout << ++k; // First: k is increased by 1, becomes 4, // Second: 4 outputs to the screen
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,
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
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
Increment and Decrement • 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 7cout << i << " " << k << endl; int j = ++k; // j is 8, k is 8cout << j << " " << k << endl;
Increment and Decrement • 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 1cout << i << " " << k << endl; int j = --k; // j is 0, k is 0cout << j << " " << k << endl;