Lecture 7 - Decision Making and Looping
Lecture 7 - Decision Making and Looping
• 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.
• They are:
– The while statement
– The do…While statement
– The for statement
True
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.
#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.
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
17
The for Loop
• The execution of the for statement is as follows:
• Example: i = 1
– The variable i is known as the loop control variable.
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