0% found this document useful (0 votes)
18 views8 pages

5 Loops

Uploaded by

Zulqurnan Anjum
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)
18 views8 pages

5 Loops

Uploaded by

Zulqurnan Anjum
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/ 8

Computer Programming Lab

Experiment 5

Exploring Various Loops used in C++

Objectives:

 To understand for , while and do-while loop


 To apply the above mentioned loops in programs

Loop:

In computer programming, a loop is a sequence of instructions that is repeated until a certain condition is
reached. There are two types of loops:

1) Entry Controlled Loops (Test condition is tested before entering into loop body e.g. for and while
loop)
2) Exit Controlled Loops (Test condition is tested at the end of loop body e.g. do-while loop)

For Loop:

A for loop is a repetition control structure that allows us to write a loop that is executed for a specific number
of times. Its syntax is:

for (initialization; condition; update/increment){ For details of for loop, refer to the class lecture

statements

The flowchart is given as:

In this flow chart, it is obvious that the initialization


expression is evaluated only once. After initialization,
the control checks the condition. If the condition is
true then control moves towards the loop body. After
execution it moves towards the update/increment
expression of for loop. Then the condition is again
verified. If the condition is true, the loop enters the
body and next iteration begins. The loop terminates
if the condition is false or has statements to terminate
the loop.

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 5

Example Codes:

The following code prints “Computer Programming Lab “for 6 times.


#include<iostream>
using namespace std;
int main(){

for(int i=0;i<=5;i++){
cout<<"Computer Programming Lab"<<endl;
}
return 0;
}
Another example is given as:

#include<iostream>
using namespace std;
int main(){
for(int i=0;i<=100;i++){
cout<<i<<endl;
}
for(int i=100;i>=0;i--){
cout<<i<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
for(int i=0;i<=500; i+=5){
cout<<i<<endl;
}
for(int i=500;i>=0; i-=5){
cout<<i<<endl;
}
return 0;
}
The following program takes 5 input numbers from user and returns their sum and average.

#include<iostream>
using namespace std;
int main(){

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 5

int sum=0,avg,num;
for(int i=0;i<5;i++){
cin>>num;
sum=sum+num;
}
avg=sum/5;
cout<<"The sum of 5 number is "<< sum<<endl;
cout<<"The average of 5 number is "<< avg<<endl;
return 0;
}
The following program takes input from user to specify the number of entries and returns their sum and
average.

#include<iostream>
using namespace std;
int main(){

int sum=0,avg,num,new_num;
cout<<"for how many numbers you want to take sum and average"<<endl;
cin>>num;
for(int i=1; i<(num+1); i++){
cout<<"plz enter the number"<<i<<endl;
cin>>new_num;
sum=sum+new_num;
}
avg=sum/num;
cout<<"The sum of 5 number is "<< sum<<endl;
cout<<"The average of 5 number is "<< avg<<endl;
return 0;
}
The following program takes integer number from a user and outputs whether it is prime or not.
#include<iostream>
using namespace std;
int main(){

int num,counter,rem;
cout<<"plz enter the number"<<endl;
cin>>num;
for(int i=1;i<=num;i++){
rem=num%i;
if(rem==0){
counter++;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 5

}
}
if(counter==2){
cout<<"The number "<<num<<" is a prime number"<<endl;
}
else
{
cout<<"the number "<<num<<" is not a prime number"<<endl;
}
return 0;
}
The multiple variable initialization can also be done in for loop with multiple increment/decrement as given as:
#include<iostream>
using namespace std;
int main(){

for(int i=0,j=10,k=20;(i+j+k)<100;j++,k--,i+=k){
cout<<i<<" "<<j<<" "<<k<<endl;
}
return 0;
}
Analyze the code in the box.
For infinite for loops:
#include<iostream>
using namespace std;
int main(){

for(; ;){
cout<<"Learning Infinite Loop"<<endl;
}

return 0;

While Loop:

While studying for loop we have seen that the number of iterations is known beforehand, i.e. the number of
times the loop body is needed to be executed is known to us. While loops are used in situations where we do
not know the exact number of iterations of the loop beforehand. The loop execution is terminated on the basis
of the test conditions. The syntax of while loop is:
Initialization expression;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 5

while (Test Condition){


statements;
update expression;
}
Here after initialization, the control moves to test the condition.
If the condition is true, then the loop body is executed followed
by increment/decrement. After this, the condition is again
verified for further execution.

Example Codes:

The following code prints “Computer Programming Lab”


For 6 times through while loop:
#include<iostream>
using namespace std;
int main(){
int i=0;
while(i<6){
cout<<"Computer Programming Lab"<<endl;
i++;
}
return 0;
}
Another example code for while loop to generate numbers as:
#include<iostream>
using namespace std;
int main(){ Analyze the code in box
int i=0;
while(i<101){
cout<<i<<endl;
i++;
}
while(i>=0){
cout<<i<<endl;
i--;
}

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 5

return 0;
}
The following code generates the numbers with a difference of 5 using while loop as shown:
#include<iostream>
using namespace std;
int main(){
int i=0;
while(i<501){
cout<<i<<endl;
i+=5;
}
while(i>=0){
cout<<i<<endl;
i-=5;
}
return 0;
}
The following program takes input from user and returns the
sum of the entered number.
#include<iostream>
using namespace std;
int main(){
int num,rem,sum,num1; Analyze the code in box
cout<<"plz enter the number"<<endl;
cin>>num;
num1=num;
while(num>0){
rem=num%10;
num=num/10;
sum=sum+rem;
}
cout<<"the sum of entered "<<num1<< " number is "<<sum<<endl;
return 0;
}
The infinite while loop is given as:
#include<iostream>
using namespace std;
int main(){
while(1){
cout<<"Learning Infinite Loop";

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 5

}
return 0;
}

Do-While Loop:

In Do-while loops also the loop execution is terminated on the basis of test conditions. The main difference
between a do-while loop and the while loop is in the do-while loop the condition is tested at the end of the
loop body, i.e. do-while loop is exit controlled whereas the other two loops are entry-controlled loops.
In a do-while loop, the loop body will execute at least once irrespective of the test condition. The syntax for
do-while loop is:
do{
statements;
update expression;
}while(test condition);

Here first the loop body is executed after which the condition is verified.
That is why it is called as exit control loop. If the condition is true, then
It will again execute the loop body else it will exit the loop body.

Example Codes:

The following code gives idea of do-while loop:

#include<iostream>
using namespace std;
int main(){
int i=2;
do{
cout<<"Computer Programming Lab"<<endl;
i++;
}while(i<2);
return 0;
}
The following code prints “Computer Programming Lab” for 6 times through do-while loop.

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 5

#include<iostream>
using namespace std;
int main(){
int i=0;
do{
cout<<"Computer Programming Lab"<<endl;
i++;
}while(i<6);
return 0;
}
The infinite do-while loop is given as:

#include<iostream>
using namespace std;
int main(){
int i=0;
do{
cout<<"Computer Programming Lab"<<endl;
i++;
}while(1);
return 0;
}

Tasks:

 Write a program that keeps on taking integer input from user and returns the total number of inputs taken
from user.
 Write a program that takes input from user and outputs its factorial.
 Write a program that takes a number input from user and then returns the sum of its odd digits and even
digits.

Conclusion:

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah

You might also like