الشريحة 1

Looping II
(for statement)
Outline
 for statement
 Nested loops
 Compound assignment operators
 Increment and decrement operators
CSCE 106
2
while Statement
for Statement
 Syntax:
while (loop repetition condition)
statement;
 E.g.
Intialising loop
control variable
i = 1;
while (i <= 10)
for (i = 1; i <= 10; i = i + 1)
Testing loop control
{
cout << “*”;
variable
cout << “*”;
Updating loop control
i = i + 1;
variable
}
CSCE 106
3
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
4
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
5
#include <iostream>
using namespace std;
 Design
START
INPUT
n
void main ()
{
int n, i, sum;
cin >> n;
i=1
sum = 0
OUTPUT False
sum
i = 1;
sum = 0;
while (i <= n)
{
i <= n ?
True
STOP
CSCE 106
sum = sum + i;
i = i +1;
sum = sum + i
i=i+1
}
}
cout << “The sum is “<<sum;6
#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
7
#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
8
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
9
Nested Loops (cont’d)
Write a program fragment that uses nested
loops to produce the following graphics:
a)
**********
**********
**********
**********
CSCE 106
for (row = 0; row <= 3; row = row + 1)
{
for (col = 0; col <= 9; col = col +1)
cout << ‘*’;
cout << endl;
}
10
Nested Loops (cont’d)
b)
*
**
***
****
CSCE 106
for (row = 0; row <= 3; row = row + 1)
{
for (col = 0; col <= row; col = col +1)
cout << ‘*’;
cout << endl;
}
11
Nested Loops (cont’d)
c)
0123456789
0123456789
0123456789
0123456789
CSCE 106
for (row = 0; row <= 3; row = row + 1)
{
for (col = 0; col <= 9; col = col +1)
cout << col;
cout << endl;
}
12
Nested Loops (cont’d)
d)
0
01
012
0123
CSCE 106
for (row = 0; row <= 3; row = row + 1)
{
for (col = 0; col <= row; col = col +1)
cout << col;
cout << endl;
}
13
Compound Assignment
Operators
 General form of
compound/special
assignment operators
variable = variable op exp.;
 General form of
common operations
variable op= exp.;
 E.g.
totalPay = totalPay + pay;
product = product * item;
countEmp = countEmp+1;
time = time - 1;
CSCE 106
+= -=
 E.g.
*=
/=
%=
totalPay += pay;
product *= item;
countEmp += 1;
time -= 1;
14
Listing 5.3
CSCE 106
Using a for statement in a counting
loop
15
Listing 5.5
Converting Celsius to
Fahrenheit
CSCE 106
16
Listing 5.5
CSCE 106
Converting Celsius to Fahrenheit
(continued)
17
Output - Celsius to Fahrenheit
Celsius
10
5
0
-5
CSCE 106
Fahrenheit
50.00
41.00
32.00
23.00
18
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
19
#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
20
Listing 5.13
CSCE 106
Nested for loop program
21
Listing 5.13
CSCE 106
Nested for loop program
(continued)
22
Listing 5.14
CSCE 106
Displaying the multiplication
table
23
Listing 5.14
CSCE 106
Displaying the multiplication table
(continued)
24
Increment and Decrement
Operators (cont’d)
 Prefix operator

E.g.
m = 3;
n = ++m;
 Postfix operator

E.g.
CSCE 106
m = 3;
n = m++;
25
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
26
Exercise (cont’d)
Solution:
1
4
10
22
45
91
CSCE 106
27
Next lecture we will continue
Looping control construct in C++
CSCE 106
28