2. ITERATION STATEMENTS
Parts of a loop
• Initialization expression – Control / Counter Variable has to
be initialized before entering inside the loop. This
expression is executed only once.
• Test expression – The truth value of this expression decides
whether the loop has to be further executed or not
• Update expression – This changes the value of the control /
counter variable. This is executed at the end of loop
statements
• Body of the loop – Set of loop statements are executed
based on the condition.
3. ITERATION STATEMENTS -
FOR• Syntax
• Example
This program prints 1 to 10
for (initialization expr ; test expr ; update expr)
body of the loop;
for (i = 1; i <= 10; i=i+1)
{
cout << i ;
}
4. ITERATION STATEMENTS -
FOR
• This will print -2
• This will loop for i values 0, 1, 2, 3, 4
for (a= 10; a >= 0; a=a-3);
cout << a;
for (i = 0; i < 5; i=i+1)
cout << i * i;
Note the semicolon here.
This means empty loop
The loop has empty body
5. Misc
Declaration of variables in the loop –
Variables declared inside the loop are
accessible inside the loop only. They
cannot be accessed outside. This is called
local scope. (This applies for the selection
statements also)
int c;
for (c = 0; c <=10; c = c+1)
{
int j = c;
cout << j << “ “ << c ;
}
cout << j << “ “<< c;
Valid
Invalid
6. ITERATION STATEMENTS -
WHILE
• The syntax is
• In the while loop, the control variable
should be initialized outside the loop and it
should be updated inside the loop
while (expression)
Loop body
int a = 0;
while (a <= 10)
{
cout << a;
a = a + 1;
}
This program will print 0 to 10
7. Nested Loops• A loop may contain another loop inside its body. It is called
Nested Loop.
It is used when one variable has to change values for each value
of another variable.
Output will be
1 table starts
1 x 1 = 1
1 x 2 = 2
….
1 x 20 = 20
1 table ends
2 table starts
2 x 1 = 2
2 x 2 = 4
….
2 x 20 = 40
2 table ends
upto
10 table ends
for (int i = 1 ; i <= 10; i=i+1)
{
cout << i << “ table starts ”;
for (int j = 1 ; j <= 20; j=j+1)
cout << i << “ x “ << j << “ = “ << i * j ;
cout << i << “table ends” ;
cout << endl;
}
8. Nested Loops
Same program with while loop
int i = j = 1;
while (i <= 10)
{
cout << i << “table starts” ;
while (j <= 20)
{
cout << i << “ x “ << j << “ = “ << i * j ;
j = j + 1;
}
cout << i << “table ends” ;
cout << endl;
i = i + 1;
}
9. Nested Loop concept
• Identify the varying factors
• Design a loop for each varying factor
• For example, for a pattern like
*******
*****
***
*
• The varying factors are the line
numbers, the spaces in each line and
the number of stars in each line.
• In each line, the number of spaces
varies from 1 to m
• In each line, the number of stars varies
from 1 to n
• So the loop would look one given in
the next slide
Line
No
Total
No of
Spaces
(m)
Total
No of
stars
(n)
1 0 7
2 1 5
3 2 3
4 3 1
10. int m = 0;
int n = 7;
for (int i = 1 ; i <= 4; i=i+1)
{
for (int j = 0 ; j < m; j=j+1)
{
cout << “ ” ;
}
m = m + 1;
for (int k = 1 ; k <= n; k=k+1)
{
cout << “*” ;
}
n = n – 2;
cout << endl ;
}
Loop starts for each line
Loop starts for spaces
Loop starts for stars
For going to the next line
11. Nested Loop Example
• For example, for a pattern like
1
121
12321
1234321
• The varying factors are the line
numbers, the spaces in each line, the
numbers increasing and the numbers
decreasing.
• In each line, the number of spaces
varies from 1 to m
• In each line, the number increases upto
the line no
• In each line, the number increases from
the line no
Line No Total No
of
Spaces
(m)
Number
increasin
g till
Number
decreasi
ng from
1 3 1 -
2 2 2 1
3 1 3 2
4 0 4 3
12. int m = 3;
for (int i = 1 ; i <= 4; i=i+1)
{
for (int j = 1 ; j <= m; j=j+1)
{
cout << “ ” ;
}
m = m - 1;
for (int k = 1 ; k <= i; k=k+1)
{
cout << k ;
}
for (int z = i-1 ; z >= 1; z=z-1)
{
cout << z ;
}
cout << endl ;
}
Loop starts for each line
Loop starts for spaces
Loop starts for increasing numbers
For going to the next line
Loop starts for decreasing numbers
13. Question Paper pattern
• Write a program
– Simple loops
• Print a series (odd numbers, even numbers, 1 2 4 8
… 1024, a …. z, A…. Z, Factorial of a number,
Sum of a given series of numbers )
• Print a table (8 table)
– Nested loops
• Print a pattern
• Print tables
– Combination of if and loop
14. Find the mistakes
Wrong Code
for (int c = 0; c >= 10; c = c + 1);
cout << c;
Wrong Code
int a;
while (a <= 10)
{
cout << a;
}
Corrected Code
for (int c = 0; c <= 10; c = c + 1)
cout << c;
Corrected Code
int a = 1;
while (a <= 10)
{
cout << a;
a = a + 1;
}
15. Find the Output
int sum = 0;
for (int c = 1; c < 10; c = c + 3)
sum = sum + c;
cout << sum << “ “ << c << endl;
Ans: sum = 0 initially
c = 1 sum = 1 in first iteration
c = 4 sum = 5 in second iteration
c = 7 sum = 12 in third iteration
c = 10
Since c < 10, so next iteration will not go inside the
loop
Output will be :
16. Find the Output
int k = 0, c;
while ( k <= 10)
{
c = k + 3;
k = k + 2;
}
cout << c << “ “ << k;
Ans: k = 0 initially
c = 3 k = 2 in first iteration
c = 5 k = 4 in second iteration
c = 7 k = 6 in third iteration
c = 9 k = 8 in fourth iteration
c = 11 k = 10 in fifth iteration
c = 13 k = 12 in sixth iteration
Since k <= 10, next iteration will not go inside the
loop as k becomes 12
Output will be :
13 12
17. Fill in the blanks
This is a program to find the sum of odd numbers till 100.
int sum = _____;
int i = 1;
while ( _________ )
{
sum = sum + i;
___________
}
cout << ____________ ;