1 / 28

Looping II ( for statement)

Learn about for and while loops, compound assignment operators, and increment/decrement operators in C++. Explore nested loops and practice with examples. Develop algorithms for calculating series sums.

Download Presentation

Looping II ( for statement)

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. Looping II(for statement)

  2. Outline • for statement • Nested loops • Compound assignment operators • Increment and decrement operators CSCE 106

  3. for Statement while Statement • Syntax: while (loop repetition condition) statement; • E.g. i = 1; while(i <= 10) { cout << “*”; i = i + 1; } Intialising loop control variable for (i = 1; i <= 10; i = i + 1) cout << “*”; Testing loop control variable Updating loop control variable CSCE 106

  4. for Statement • It is used for a counter controlled loop • Syntax: for(initializing expression; loop repetition condition; update expression) statement; • condition test precedes the execution of the loop body • loop body may not be executed at all CSCE 106

  5. An Old Exercise • Problem Analyse, design, and implement an algorithm that calculates and outputs the following sum: sum = 1 + 2 + 3 + ……. + n up to any number (n) input by the user. • Analysis • Input n: upper limit • Output sum: sum of series • Intermediate variables i: the current iteration number CSCE 106

  6. #include <iostream> using namespace std; void main () { int n, i, sum; cin >> n; i = 1; sum = 0; while (i <= n) { sum = sum + i; i = i +1; } cout << “The sum is “<<sum; } START • Design INPUT n i = 1 sum = 0 OUTPUT sum False i <= n ? True STOP sum = sum + i i = i + 1 CSCE 106

  7. #include <iostream> • using namespace std; • void main() • { • int i, n, sum = 0; • cout << “Please enter a whole number:“ << endl; • cin >> n; • for (i=1; i <= n; i = i + 1) • sum = sum + i; • cout << “The summation is:“ << sum << endl; • } CSCE 106

  8. #include <iostream> • using namespace std; • void main() • { • int i, n, sum = 0; • cout << “Please enter a whole number:“ << endl; • cin >> n; • for (i=n; i >= 1; i = i – 1) • sum = sum + i; • cout << “The summation is:“ << sum << endl; • } CSCE 106

  9. Nested Loops • As with if statements, loop statements can be nested • Each time outer loop is repeated, any inner loop is restarted - loop control components are reevaluated and all required iterations are performed CSCE 106

  10. Nested Loops (cont’d) Write a program fragment that uses nested loopsto produce the following graphics: a) ********** ********** ********** ********** for (row = 0; row <= 3; row = row + 1) { for (col = 0; col <= 9; col = col +1) cout << ‘*’; cout << endl; } CSCE 106

  11. Nested Loops (cont’d) for (row = 0; row <= 3; row = row + 1) { for (col = 0; col <= row; col = col +1) cout << ‘*’; cout << endl; } b) * ** *** **** CSCE 106

  12. Nested Loops (cont’d) for (row = 0; row <= 3; row = row + 1) { for (col = 0; col <= 9; col = col +1) cout << col; cout << endl; } c) 0123456789 0123456789 0123456789 0123456789 CSCE 106

  13. Nested Loops (cont’d) for (row = 0; row <= 3; row = row + 1) { for (col = 0; col <= row; col = col +1) cout << col; cout << endl; } d) 0 01 012 0123 CSCE 106

  14. Compound Assignment Operators • General form of compound/special assignment operators variable op= exp.; += -= *= /= %= • E.g. totalPay += pay; product *= item; countEmp += 1; time -= 1; • General form of common operations variable = variable op exp.; • E.g. totalPay = totalPay + pay; product = product * item; countEmp = countEmp+1; time = time - 1; CSCE 106

  15. Listing 5.3Using a for statement in a counting loop CSCE 106

  16. Listing 5.5Converting Celsius to Fahrenheit CSCE 106

  17. Listing 5.5Converting Celsius to Fahrenheit (continued) CSCE 106

  18. Output - Celsius to Fahrenheit Celsius Fahrenheit 10 50.00 5 41.00 0 32.00 -5 23.00 CSCE 106

  19. Increment and Decrement Operators ++ -- • E.g.: • ++i (instead of i = i + 1) • --i (instead of i = i - 1) • Applied to a single variable • Side effect: a change in the value of a variable (by one) as a result of carrying out an operation • Often used to update loop control variable CSCE 106

  20. #include <iostream> • using namespace std; • void main() • { • int i, n, factorial = 1; • cout << “Please enter a whole number:“; • cin >> n; • i = 2; • while (i <= n) • { • factorial = factorial * i; • i++; // instead of i= i + 1; • } • cout << “The factorial is:“ << factorial << endl; • } CSCE 106

  21. Listing 5.13Nested for loop program CSCE 106

  22. Listing 5.13Nested for loop program (continued) CSCE 106

  23. Listing 5.14Displaying the multiplication table CSCE 106

  24. Listing 5.14Displaying the multiplication table (continued) CSCE 106

  25. Increment and Decrement Operators (cont’d) • Prefix operator • E.g. m = 3; n = ++m; • Postfix operator • E.g. m = 3; n = m++; CSCE 106

  26. Exercise What is the output from the following C++ segment? int x = 1; while (x <= 10) { cout<<x <<endl; x += ++x; } while (x <= 91) { cout<<x <<endl; x += x++; } CSCE 106

  27. Exercise (cont’d) Solution: 1 4 10 22 45 91 CSCE 106

  28. Next lecture we will continue Looping control construct in C++ CSCE 106

More Related