5 Loops
5 Loops
Experiment 5
Objectives:
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
Example Codes:
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(){
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++;
}
}
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;
Example Codes:
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";
}
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:
#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.
#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: