1 / 13

Understanding For Loops in C++: Syntax, Initialization, and Examples

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.

forrest
Download Presentation

Understanding For Loops in C++: Syntax, Initialization, and Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming for Loops

  2. The for Statement initialization condition false true action update

  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;

  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;

  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;

  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. 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.

  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 2k = 3;cout << ++k; // First: k is increased by 1, becomes 4, // Second: 4 outputs to the screen

  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. 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

  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. 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;

  13. 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;

More Related