[go: up one dir, main page]

0% found this document useful (0 votes)
22 views27 pages

Lecture 7 - Decision Making and Looping

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views27 pages

Lecture 7 - Decision Making and Looping

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Lecture 7

Decision Making and Looping


Objectives
• Discuss the working of:
– while loop
– do…while loop
– for loop

• Clarify the difference between the while loop


and do…while loop

• Explain the types of for loops:


– Simple for loop
– Nested for loops
2
Decision Making and Looping
• Sequence of statements are executed until a
condition is satisfied
• A loop consists of two segments:
– body of the loop.
– control statement / test condition.

• Depending on the position of the control


statement in the loop, a loop may be classified
as an entry-controlled loop or as an exit-
controlled loop.
Decision Making and Looping
• An entry-controlled loop:
– First the condition is checked and the body of the
loop is executed if the result of the condition is
true.

• An exit-controlled loop:
– First the body of the loop is executed and the
condition is checked at the end of the loop.
Decision Making and Looping
• A looping process would involve the following
four steps:
– Setting and initialization of a counter.

– Test for a specified condition for execution of the


loop.

– Execution of the statements in the loop.

– Incrementing or decrementing the counter.


5
Decision Making and Looping
• C++ language provides three loop constructs for
performing loop operations.

• They are:
– The while statement
– The do…While statement
– The for statement

• The while and for statements are entry controlled


statements.

• The do…while statement is an exit controlled


statement. 6
The while Loop
• The general syntax of the while loop is as
follows:
while (test condition)
{
body of the loop
}
Test False
condition

True

Body of the Loop

7
The while Loop
• First the condition is evaluated and if the result
of the condition is true the body of the loop is
executed.

• After the first iteration, the control variable is


either incremented or decremented and the
condition is checked again.

• This process repeats until the test condition


finally becomes false and the control is
transferred out of the loop.
8
The while Loop
• On exit, the program continues with the statement
immediately after the body of the loop.

• The body of the loop may have one or more


statements.

• Curly brackets are needed if the body of the loop


contains two or more statements.

• However, it is a good practice to use curly


brackets even if the body has only one statement.
9
Example: while Loop
• Write a C++ program using while loop to list numbers from
1 to 100.

#include<iostream>
using namespace std;
int main()
{
int number = 1;
while(number <= 100)
{
cout<<number<<endl;
number = number + 1;
}
}
10
Example: while Loop
• Write a C++ program using while loop to list numbers from
100 to 1.

#include<iostream>
using namespace std;
int main()
{
int number = 100;
while(number > 0)
{
cout<<number<<endl;
number = number - 1;
}
}
11
The do…while Loop
• The body of the loop will execute before checking the
condition.

• If the result of the condition is true, it will execute the


body of the loop once again.

• If the result of the condition is false, it will exit from


the loop.

• The general syntax of the do…while loop is:


do
{
body of the loop
}
while(test condition);
12
The do…while Loop

Body of the Loop

Test True
Condition

False

13
Example: do…while Loop
#include<iostream>
using namespace std;
int main()
{
int max, sum = 0, digit = 2;
cout<<"Enter the maximum number ";
cin>>max;
do
{
sum = sum + digit;
digit += 2;
}
while(digit<=max);
cout<<"Sum of a set of even numbers = "<<sum;
}

14
Example: do…while Loop
#include<iostream>
using namespace std;
int main()
{
int sum = 0, digit = 10;
do
{
sum = sum + digit;
digit--;
}
while(digit>0);
cout<<"Sum of the numbers = "<<sum;
}
15
The for Loop
• Simple for loop- only one for loop
• The general syntax of the for loop is:

for(initialization;condition;increment/decrement)
{
body of the loop
}

16
The for Loop

Test False
condition

True

Body of the Loop

17
The for Loop
• The execution of the for statement is as follows:

• Initialization of the loop control variable is done first.

• Example: i = 1
– The variable i is known as the loop control variable.

• The value of the loop control variable is tested using


the test-condition.

• If the result of the condition is true, the body of the


loop is executed; otherwise the loop is terminated.
18
The for Loop
• After executing the body of the loop, control is
transferred back to the for loop and evaluate the last
expression of the for loop.

• Control variable is incremented or decremented using


an assignment statement such as i++ or i-- and the new
value of the control variable is again tested to see
whether it satisfy the condition of the loop.

• If the result of the condition is true, the body of the


loop is again executed.

• This process continues till the value of the control


variable fails to satisfy the test-condition.
19
Example: for Loop
#include<iostream>
using namespace std;
int main()
{
int number, sum =0;
cout<<"Enter the number of elements ";
cin>>number;
for(int i=1;i<=number;i++)
{
sum = sum + i;
}
cout<<"\nValue of sum = "<<sum;
}

20
Example: for Loop
#include<iostream>
using namespace std;
int main()
{
int i;
for(i = 100; i > 0; i --)
{
if( i % 5 == 0)
cout<<i<<endl;
}
}

21
Additional Features of for Loop
• More than one variable can be initialized at a time in the for
loop separated by comma.

#include<iostream>
using namespace std;
int main()
{
for(int i = 1, j = 12; i<=12; i++)
{
int mul = i * j;
cout<<mul<<endl;
}
}

22
Additional Features of for Loop
• The increment / decrement section may also have more
than one part separated by comma.

#include<iostream>
using namespace std;
int main()
{
for(int a= 2, b=30; a<b; a++, b--)
{
cout<<"Value of a = "<<a<<endl;
cout<<"Value of b = "<<b<<endl;
}
}
23
Additional Features of for Loop
• It is also permissible to use expressions in the
initialization section and increment / decrement section.

#include<iostream>
using namespace std;
int main()
{
int a = 2;
for(a=a+0; a<=20; a=a+2)
{
cout<<"Value of a = "<<a<<endl;
}
}
24
Additional Features of for Loop
• The test-condition of the for loop may have a logical
expression.

#include<iostream>
using namespace std;
int main()
{
int sum = 0;
for(int i=1,j=10;(i<20)&&(j>0);i++,j--)
{
sum = sum + i + j;
cout<<"Sum = "<<sum<<endl;
}
}
25
Additional Features of for Loop
• One or more sections of the for loop can be omitted.

#include<iostream>
using namespace std;
int main()
{
int m = 5;
for(; m <= 100;)
{
cout<<"Valur of M = "<<m<<endl;
m = m + 5;
}
}
26
Additional Features of for Loop
• One for statement within another for statement is known as nesting
of for loops.

#include<iostream>
using namespace std;
int main()
{
for(int i = 0; i <3; i++)
{
for(int j = 0; j<5; j++)
{
cout<<"\nThis is INNER LOOP";
}
cout<<"\nThis is OUTER LOOP"<<endl;
}
}
27

You might also like