Loops: Control Structures II (Repetition) Introduction To Programming Dr. Sajid Khan
Loops: Control Structures II (Repetition) Introduction To Programming Dr. Sajid Khan
2
Why Is Repetition Needed?
3
The while Loop
4
The while Loop (continued)
5
The while Loop (continued)
6
7
Counter-Controlled while Loops
• If you know exactly how many pieces of data need to be read, the
while loop becomes a counter-controlled loop
• The syntax is:
counter = 0;
while(counter < N)
{
.
counter++;
.
}
8
Example 1
9
Example 2
10
Example 3
11
• int counter = 1, num, sum = 0;
• while(counter<=5)
•{
• cout<<"Please enter "<<counter<<"th number"<<endl;
• cin>>num;
• sum = sum + num;
• counter++;
•}
• cout<<"Sum of entered numbers is "<<sum<<endl;
12
Sentinel-Controlled while Loops
13
Example
• Design a program that stays alive when user enter number other
than 100, if user enter 100, the program should terminate
• int num;
• cout<<"Please enter any number other than 100"<<endl;
• cin>>num;
• while(num!=100)
•{
• cout<<"Its good that you want to be with us"<<endl;
• cout<<"Please enter any number other than 100 to continue"<<endl;
• cin>>num;
•}
14
Flag-Controlled while Loops
• A flag-controlled while loop uses a Boolean variable to control the
loop
• The flag-controlled while loop takes the form:
found = false;
while(!found)
{
.
if(expression)
found = true;
.
}
15
Example
16
• int num, sum = 0;
• bool flag = true;
• while(flag)
• {
• cout<<"Please enter a positive number"<<endl;
• cin>>num;
• if(num>=0)
• {
• sum = sum + num;
• }
• else
• {
• cout<<"You entered a negative number. \nThis program is about to
terminate"<<endl;
• flag = false;
•}
•}
17
The for Loop
18
19
The for Loop (continued)
20
The for Loop (continued)
21
The for Loop (continued)
22
The for Loop (continued)
for(;;)
cout<<"Hello"<<endl;
23
Examples
24
Example 1
• for(int i = 1;i<=10;i++)
•{
• cout<<"Hello"<<endl;
•}
25
• Where i++ is post increment of i.
26
• There are two ways to initialize the counter i.
• For example, the above example can be implemented using
• int i;
• for(i = 1;i<=10;i++)
•{
• cout<<"Hello"<<endl;
•}
27
Difference between two approaches?
28
First case
• for(int i = 1;i<=10;i++)
•{
• cout<<"Hello"<<endl;
•}
• cout<<"Final value of counter is "<<i<<endl;
29
Second case
• int i;
• for(i = 1;i<=10;i++)
•{
• cout<<"Hello"<<endl;
•}
• cout<<"Final value of counter is "<<i<<endl;
30
Difference?
• In first case, the counter i is local variable of for loop that can be
accessed inside the loop body.
• Immediately after loop is executed completely, the counter i is
destroyed. That’s why we can’t access it outside the loop body.
• In second case, counter i is local variable of main body, since main
contain for loop as well, that’s why it is accessible inside the loop
body as well.
31
Example 2
32
Example 3
33
Example 4
• Design a program that stays alive when user enter number other than 100, if
user enter 100, the program should terminate
34
Example 5
35
• int num, sum = 0;
• for(int i = 1;i<2;)
•{
• cout<<"Enter any positive number"<<endl;
• cin>>num;
• if(num>0)
• sum = sum + num;
• else
• i = 2;
•}
• cout<<"Final sum is "<<sum<<endl;
36
The do…while Loop
37
The do…while Loop (continued)
38
39
Example
• Design a program that prompt user to enter numbers until user type
‘N’
40
Break & Continue Statements
41
Break & Continue Statements (continued)
42
Break & Continue Statements (continued)
• It skips remaining statements and proceeds with the next iteration of the
loop
43
Break & Continue Statements (continued)
44
Example of continue
45
Example of break
• Design a program that stays alive when user enter number other than 100, if
user enter 100, the program should terminate
• int num;
• cout<<"Please enter any number other than 100"<<endl;
• cin>>num;
• while(true)
•{
• if(num == 100)
• break;
• cout<<"Its good that you want to be with us"<<endl;
• cout<<"Please enter any number other than 100 to continue"<<endl;
• cin>>num;
•}
• cout<<"You came out of the loop"<<endl;
46
Nested Control Structures
47
Nested Control Structures (continued)
• Since five lines are to be printed, we start with the following for
statement
for(i = 1; i <= 5 ; i++)
• The value of i in the first iteration is 1, in the second iteration it is 2,
and so on
• Can use the value of i as limit condition in another for loop nested
within this loop to control the number of starts in a line
48
Nested Control Structures (continued)
49
Nested Control Structures (continued)
• What pattern does the code produce if we replace the first for
statement with the following?
for (i = 5; i >= 1; i--)
• Answer:
*****
****
***
**
*
50
Summary
• C++ has three looping (repetition) structures: while, for, and do…
while
• while, for, and do are reserved words
• while and for loops are called pre-test loops
• do...while loop is called a post-test loop
• while and for may not execute at all, but do...while always executes
at least once
51
Summary
• while: expression is the decision maker, and the statement is the
body of the loop
• In a counter-controlled while loop,
• Initialize counter before loop
• Body must contain a statement that changes the value of the
counter variable
• A sentinel-controlled while loop uses a sentinel to control the while
loop
• An EOF-controlled while loop executes until the program detects
the end-of-file marker
52
Summary
53