Professional Documents
Culture Documents
6-Lecture-FoP-Repetition structures
6-Lecture-FoP-Repetition structures
Dr Ayesha Zeb
Email: ayesha.zeb@ceme.nust.edu.pk
Lecture Contents
• Repetition Structures
– While Loop
– For Loop
– Do-While Loop
Repetition Structures
• Repetition structures specify that actions should be
repeated while some condition remains true
– while
– for
– do while
while Loop
• while loop executes a set of
statements as long as the condition
specified at the beginning is true.
• The condition is evaluated at the
beginning of the loop
– If it is true, the loop will execute
– If it is false, the loop will not execute
even once
while Loop
Syntax:
while(condition)
{
statement1;
…
statement n;
}
while Loop
Example: Initialization Output:
1
int a=1; Condition 2
3
while(a<=3)
{
cout<<a<<endl;
a++;
Increment
}
• As long as the condition continues to be true (i.e as long
as a<=3), the statement within the loop will continue to be
executed over and over again.
Steps of Loop Execution
• Initialization occurs before the while statement.
• Loop entry occurs the moment the flow of control
reaches the first statement inside the loop body.
• Each time the body of the loop is executed, a pass
is made through the loop. This pass is called an
iteration.
• Before each iteration, the loop test is made at the
beginning of the loop.
• When the last iteration is complete, the flow of
control will be passed to the first statement after the
loop, which means the program has exited the
loop.
• The condition that causes the loop to exit is called
the exit condition.
Counter-Controlled Loops
Condition
Loop Body
Variable
increment
for Loop
Syntax
• for ( ; ; ) • for ( ; ; )
{ statement1; { statement1;
statement2; if(condition)
} break;
}
For Loop
• for ( ; ; ) • for ( ; ; )
{ statement1; { statement1;
statement2; if(condition)
} break;
}
Think-Pair-Share Activity
• Vary the control variable from 1 to 100 in increments of 1.
• Vary the control variable from 100 down to 1 in increments of -
1 (that is, decrements of 1).
• Vary the control variable from 7 to 77 in steps of 7.
• Vary the control variable from 20 down to 2 in steps of -2.
• Vary the control variable over the following sequence of
values: 2, 5, 8, 11, 14, 17, 20.
• Vary the control variable over the following sequence of
values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0.
Class Activity
• Vary the control variable from 1 to 100 in increments of 1.
• for ( int i = 1; i <= 100; i++ )
• Vary the control variable from 100 down to 1 in increments of -1 (that is,
decrements of 1).
• Vary the control variable over the following sequence of values: 99, 88, 77,
66, 55, 44, 33, 22, 11, 0.
• Vary the control variable from 1 to 100 in increments of 1.
• for ( int i = 1; i <= 100; i++ )
• Vary the control variable from 100 down to 1 in increments of -1 (that is,
decrements of 1).
• for ( int i = 100; i >= 1; i-- )
• Vary the control variable from 7 to 77 in steps of 7.
• Vary the control variable over the following sequence of values: 99, 88,
77, 66, 55, 44, 33, 22, 11, 0.
• Vary the control variable from 1 to 100 in increments of 1.
• for ( int i = 1; i <= 100; i++ )
• Vary the control variable from 100 down to 1 in increments of -1 (that is,
decrements of 1).
• for ( int i = 100; i >= 1; i-- )
• Vary the control variable from 7 to 77 in steps of 7.
• for ( int i = 7; i <= 77; i += 7 )
• Vary the control variable from 20 down to 2 in steps of -2.
•
• Vary the control variable over the following sequence of values: 2, 5, 8,
11, 14, 17, 20.
• Vary the control variable over the following sequence of values: 99, 88,
77, 66, 55, 44, 33, 22, 11, 0.
• Vary the control variable from 1 to 100 in increments of 1.
• for ( int i = 1; i <= 100; i++ )
• Vary the control variable from 100 down to 1 in increments of -1 (that is,
decrements of 1).
• for ( int i = 100; i >= 1; i-- )
• Vary the control variable from 7 to 77 in steps of 7.
• for ( int i = 7; i <= 77; i += 7 )
• Vary the control variable from 20 down to 2 in steps of -2.
• for ( int i = 20; i >= 2; i -= 2 )
• Vary the control variable over the following sequence of values: 2, 5, 8,
11, 14, 17, 20.
• Vary the control variable over the following sequence of values: 99, 88,
77, 66, 55, 44, 33, 22, 11, 0.
• Vary the control variable from 1 to 100 in increments of 1.
• for ( int i = 1; i <= 100; i++ )
• Vary the control variable from 100 down to 1 in increments of -1 (that is,
decrements of 1).
• for ( int i = 100; i >= 1; i-- )
• Vary the control variable from 7 to 77 in steps of 7.
• for ( int i = 7; i <= 77; i += 7 )
• Vary the control variable from 20 down to 2 in steps of -2.
• for ( int i = 20; i >= 2; i -= 2 )
• Vary the control variable over the following sequence of values: 2, 5, 8,
11, 14, 17, 20.
• for ( int i = 2; i <= 20; i += 3 )
• Vary the control variable over the following sequence of values: 99, 88,
77, 66, 55, 44, 33, 22, 11, 0.
• Vary the control variable from 1 to 100 in increments of 1.
• for ( int i = 1; i <= 100; i++ )
• Vary the control variable from 100 down to 1 in increments of -1 (that is,
decrements of 1).
• for ( int i = 100; i >= 1; i-- )
• Vary the control variable from 7 to 77 in steps of 7.
• for ( int i = 7; i <= 77; i += 7 )
• Vary the control variable from 20 down to 2 in steps of -2.
• for ( int i = 20; i >= 2; i -= 2 )
• Vary the control variable over the following sequence of values: 2, 5, 8,
11, 14, 17, 20.
• for ( int i = 2; i <= 20; i += 3 )
• Vary the control variable over the following sequence of values: 99, 88,
77, 66, 55, 44, 33, 22, 11, 0.
• for ( int i = 99; i >= 0; i -= 11 )
What is this program doing?
• Eg. Pattern:
1
12
123
1234
Nested For Loop
• Eg. Pattern:
1
12
123
1234
• Logic:
– 4 levels ->
Nested For Loop
• Eg. Pattern:
1
12
123
1234
• Logic:
– 4 levels -> outer loop will run how many times?
Nested For Loop
• Eg. Pattern:
1
12
123
1234
• Logic:
– 4 levels → outer loop will run FOUR times! 0<i<4
Nested For Loop
• Eg. Pattern:
1
12
123
1234
• Logic:
– 4 levels → outer loop will run FOUR times! 0<i<4
→ Inner loop will run how many times?
int main()
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i; j++)
{
cout<<j;
}
cout<<endl;
}
}
int main()
{
for(int i=4;i>0;i--)
{
for(int j=0;j<i; j++)
{
cout<<“*”;
}
cout<<endl;
}
}
Nested For Loop
• The outer loop runs the number of times the pattern has
to be repeated (in the above case 4 times). Thus it can
run from 1 to 4, from 4 to 1, from 2 to 6 etc.
• The inner loop depends on the values being displayed,
and often has to be related to the outer loop.
Nested For Loop
Syntax:
for(x=0;x<3;x++)
for(y=0;y<3;y++)
for(z=0;z<5;z++)
{ statement1;
statement2;
statement3;
}
Try Me!!