Download presentation
Presentation is loading. Please wait.
Published byNaomi Bell Modified over 8 years ago
1
Looping II (for statement)
2
CSCE 1062 Outline for statement Nested loops Compound assignment operators Increment and decrement operators
3
CSCE 1063 while Statement Syntax: while (loop repetition condition) statement; E.g. i = 1; while (i <= 10) { cout << “*”; i = i + 1; } Intialising loop control variable Testing loop control variable Updating loop control variable for Statement for (i = 1; i <= 10; i = i + 1) cout << “*”;
4
CSCE 1064 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
5
CSCE 1065 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
6
CSCE 1066 INPUT n OUTPUT sum START STOP Design i <= n ? True False i = 1 sum = 0 sum = sum + i i = i + 1 #include 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; }
7
CSCE 1067 #include 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; }
8
CSCE 1068 #include 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; }
9
CSCE 1069 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
10
CSCE 10610 Nested Loops (cont’d) Write a program fragment that uses nested loops to produce the following graphics: a) ********** for (row = 0; row <= 3; row = row + 1) { for (col = 0; col <= 9; col = col +1) cout << ‘*’; cout << endl; }
11
CSCE 10611 Nested Loops (cont’d) b) * ** *** **** for (row = 0; row <= 3; row = row + 1) { for (col = 0; col <= row; col = col +1) cout << ‘*’; cout << endl; }
12
CSCE 10612 Nested Loops (cont’d) c) 0123456789 for (row = 0; row <= 3; row = row + 1) { for (col = 0; col <= 9; col = col +1) cout << col; cout << endl; }
13
CSCE 10613 Nested Loops (cont’d) d) 0 01 012 0123 for (row = 0; row <= 3; row = row + 1) { for (col = 0; col <= row; col = col +1) cout << col; cout << endl; }
14
CSCE 10614 Compound Assignment Operators General form of common operations variable = variable op exp.; E.g. totalPay = totalPay + pay; product = product * item; countEmp = countEmp+1; time = time - 1; General form of compound/special assignment operators variable op= exp.; += -= *= /= %= E.g. totalPay += pay; product *= item; countEmp += 1; time -= 1;
15
CSCE 10615 Listing 5.3 Using a for statement in a counting loop
16
CSCE 10616 Listing 5.5 Converting Celsius to Fahrenheit
17
CSCE 10617 Listing 5.5 Converting Celsius to Fahrenheit (continued)
18
CSCE 10618 Output - Celsius to Fahrenheit Celsius Fahrenheit 1050.00 541.00 032.00 -523.00
19
CSCE 10619 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
20
CSCE 10620 #include 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; }
21
CSCE 10621 Listing 5.13 Nested for loop program
22
CSCE 10622 Listing 5.13 Nested for loop program (continued)
23
CSCE 10623 Listing 5.14 Displaying the multiplication table
24
CSCE 10624 Listing 5.14 Displaying the multiplication table (continued)
25
CSCE 10625 Increment and Decrement Operators (cont’d) Prefix operator E.g.m = 3; n = ++m; Postfix operator E.g.m = 3; n = m++;
26
CSCE 10626 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++; }
27
CSCE 10627 Exercise (cont’d) Solution: 1 4 10 22 45 91
28
CSCE 10628 Next lecture we will continue Looping control construct in C++
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.