Loops
Loops in a programming is used to iterate the statements or run part of the program several times.
Types of Loops:
- For loop
- while loop
- do-while loop
1.For loop
For loops are used when you have a block of code which you want to repeat a fixed number of times.
syntax:
1. for(initilization; condition; inc/dec)
{// loop body start
} // loop body ends
initilization
- initializes variable and executed only once.
condition
-if true
, the body of for
loop is executed if false
, the for loop is terminated.
inc/dec
- increases or decrease the value of initialized variables and again checks the condition.
Example 1️⃣ :
#include <iostream>
using namespace std;
int main() {
int i;
for(i=0;i<5; i++)
{
cout<<"Hello \n";
}
return 0;
}
Output :
Hello
Hello
Hello
Hello
Hello
Explanation :
Line 6 : Declare i
variable.
Line 7 : for(i=0;i<5; i++)
for
is keyword, i=0
initialize i
, i<5
condition to check, i++
increase value one by one.
Line 8 : loop body starts.
Line 9 : print Hello
using cout.
Line 10 : loop body ends.
Example 2️⃣ : Reverse
#include <iostream>
using namespace std;
int main() {
int i;
for(i=4;i>0; i--)
{
cout<<"Hello \n";
}
return 0;
}
Output :
Hello
Hello
Hello
Hello
Explanation :
Line 6 : Declare i
variable.
Line 7 : for(i=4;i>0; i--)
for
is keyword, i=4
initialize i
, i>0
condition to check, i--
decrease value one by one.
Line 8 : loop body starts.
Line 9 : print Hello
using cout.
Line 10 : loop body ends.
Example 3️⃣ : Reverse
#include <iostream>
using namespace std;
int main() {
int i;
for(i=4;i>0; i--)
{
cout<<i<<"\n";
}
return 0;
}
Output :
4
3
2
1
Explanation :
Line 6 : Declare i
variable.
Line 7 : for(i=4;i>0; i--)
for
is keyword, i=4
initialize i
, i>0
condition to check, i--
decrease value one by one.
Line 8 : loop body starts.
Line 9 : print i
variable using cout.
Line 10 : loop body ends.
Infinite Loop:
- loops condition is always true .
- it always runs because stoppping condition never occurs .
Example 1️⃣ :
#include <iostream>
using namespace std;
int main() {
int i;
for(i=1;i>0; i++)
{
cout<<i<<"\n";
}
return 0;
}
Output :
1
2
3
.
.
.
.
.
infinite
Explanation :
Line 6 : Declare i
variable.
Line 7 : for(i=1;i>0; i++)
for
is keyword, i=1
initialize i
, i>0
condition to check, i++
increase value one by one.
Line 8 : loop body starts.
Line 9 : print i
variable using cout.
Line 10 : loop body ends.
Practice Program
Example 2️⃣ : WAP to print all even numbers upto n
#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter value of n: ";
cin>>n;
for(i=2;i<=n; i+2)
{
cout<<i<<"\n";
}
return 0;
}
Output :
Enter value of n: 20
2
4
6
8
10
.
.
.
20
Explanation :
Line 6 : Declare n
variable.
Line 7 : enter value of n:
from user.
Line 9 : for(i=2;i<=n; i+2)
for
is keyword, i=2
initialize i
, i<=n
condition to check, i+2
increase value by two.
Line 10 : loop body starts.
Line 11 : print i
variable using cout.
Line 12 : loop body ends.
Example 3️⃣ : WAP to print all even numbers upto n
#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter value of n: ";
cin>>n;
for(i=2;i<=n; i=i+2)
{
if(i%2 == 0)
{
cout<<i<<endl;
}
}
return 0;
}
Output :
Enter value of n: 10
2
4
6
8
10
Example 4️⃣ :WAP to print sum of n numbers ?
#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter value 0f n: ";
cin>>n;
int sum =0;
for(i=1;i<=n; i++)
{
sum = sum + i;
}
cout<<"Sum : "<<sum;
return 0;
}
Output :
Enter value of n: 5
Sum :15
Explanation :
Line 6 : Declare n
variable.
Line 7 : enter value of n:
from user.
Line 10 : initialize sum
variable.
Line 11 : for(i=1;i<=n; i++)
for
is keyword, i=1
initialize i
, i<=n
condition to check, i++
increase value by one.
Line 12 : loop body starts.
Line 13 : sum = sum + i
In sum
variable assign value of sum + i
,
Line 14 : loop body ends.
Line 15 : print sum
variable