Actually loop is a process of repeating the same process again and again without writing it multiple times.
For example if you want to take 50 inputs from user then you can use a loop to take those inputs by using the input statement only once.
The process of repeating the same thing again and again ( Loop ) is known as
Iteration.
Types of Loop:
- "For" Loop
- "While" Loop
- "Do While" Loop
"For" Loop:
"For" Loop is used when we know how many time iteration will be performed.For example if we know that we will take 50 inputs from user then we will use "For" Loop for that work .
Syntax for a "For" loops is:
for(int i=starting value;condition;increment or decrement)
{
This is the body of loop.
}
"While" and "Do While" Loop:
"While" and "Do While" Loop is used when don't know how many times the iteration will be performed.For example if we know that we want to do the given process but we have no idea that how many times we will perform this thing then we use "While" or "Do While" loop.
Syntax for "While" loop is :
while(true/condition)
{
this is the body of "while" loop.
use a break statement to end the loop . It is recommended to use a break statement in a condition.
}
Syntax for "Do-While" loop is:
do
{
This is body of "Do-While" loop.
}while(condition);