280 likes | 294 Views
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.
E N D
Outline • for statement • Nested loops • Compound assignment operators • Increment and decrement operators CSCE 106
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
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
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
#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
#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
#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
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
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
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
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
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
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
Listing 5.3Using a for statement in a counting loop CSCE 106
Listing 5.5Converting Celsius to Fahrenheit (continued) CSCE 106
Output - Celsius to Fahrenheit Celsius Fahrenheit 10 50.00 5 41.00 0 32.00 -5 23.00 CSCE 106
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
#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
Listing 5.13Nested for loop program CSCE 106
Listing 5.14Displaying the multiplication table (continued) CSCE 106
Increment and Decrement Operators (cont’d) • Prefix operator • E.g. m = 3; n = ++m; • Postfix operator • E.g. m = 3; n = m++; CSCE 106
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
Exercise (cont’d) Solution: 1 4 10 22 45 91 CSCE 106
Next lecture we will continue Looping control construct in C++ CSCE 106