Â
What are Loops?
 Loops are the programming structure that repeats a set of  instruction till a particular condition is met.
Loop play a vital role in programming and there are numerous of programs which are completely based on loops like Sum, Factorial, Palindrome, Armstrong Number and many more. There are many logics which are baseless without loops.
As you can see, the above Flowchart depicts some basic of the working of loop:
It shows that when the program reaches the loop statements then the loop syntax will not allow the program to execute further till the Requirements are met or satisfied.
How it is different from Conditional Statements ?
In conditional statements either the condition is true or false the program will proceed further or it will print or execute some statements.
But in Loops till the condition is not fulfilled the program will not proceed further it will be in a continuous cycle inside the syntax.
There are different types of Loops in Programming Language :
- for LoopÂ
- while Loop
- do-while Loop
- Nested Loops
for Loop:
For loop is a loop which will repeat a block of code till n number of times where n is the range of the loop which can be either pre-decided or during run time. It requires a iterator which is used to initialize the loop.
Syntax:-
for(initialize iterator, condition, increment/decrement)
{
//block of code to be executed
}
// Program to print a character 5 times
#include <iostream>
using namespace std;
int main()
{
int i;
char c;
cout << "Enter a symbol/character:- ";
cin >> c;
for (i = 0; i < 5; i++)
{
cout << c;
}
return 0;
}
while Loop:
While loop is a loop in which the execution is decided based on boolean condition. It is also known as repeating If statement.
Syntax:-
while(condition)
{
//code to be executed
}
// Printing Hello World 5 times
#include <iostream>
using namespace std;
int main()
{
int n = 5;
int i = 1;
while (i <= n)
{
cout << "Hello World !\n";
i++;
}
return 0;
}
do-while Loop:
In do while loop the statements or the block of code is at least executed once then the condition is checked and if the condition is satisfied then again the block of code will be executed
Syntax:-
do
{
//code block to be executed
}
while(condition);
// Printing a content n times
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter value of n to repeat printing that n times: ";
cin >> n;
do
{
cout << "Help For Coders\n";
n--;
} while (n > 0);
return 0;
}
Nested Loop:
Nested loop means a loop inside another loop. In this loop the condition of both the loop has to be satisfied for the further execution. Nested loop can be of different types like "A for loop inside another for loop", "A for loop inside a while loop " and vice-versa," A for loop inside a do-while loop" and vice-versa.
Syntax:-
for(initialize iterator, condition, increment/decrement) { for(initialize iterator, condition, increment/decrement) { //code block to be executed } }
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "\nEnter the number of rows\n";
cin >> rows;
cout << "\n";
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows; j++)
{
cout << i;
}
cout << "\n";
}
}
So, this was all about the loop, different type of loops and their syntaxes and the examples of all the loops mentioned above will be provided in further posts with the help of pattern series and other projects the reason behind this is the better understanding of using loops and their working.
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP