For performing a set of rules repeatedly we use loop
To make our code efficient and mantainable we use loop for doing some repeating work.
If we want that while a condition is true the function should keep running
Loop starts → checks the condition → true - Execute loop , iteration increases/decrease. → false - exit the loop
In do-while loops, the execution is terminated on the basis of the test condition. The main difference
between the do-while loop and while loop is that, in the do-while loop, the condition is tested at the end
of the loop body, whereas the other two loops
are entry controlled loops.
Note: In do-while loop, the loop body will execute at least once
irrespective of the test condition.
A do-while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time. Unlike for and while loops, which test the loop condition first, then execute the code written inside the body of the loop, the do-while loop checks its condition at the end of the loop.
//basic structure
do {
//code to be executed
} while (condition);
// sample code
int i = 0;
do {
i = i+1;
printf("%d",i);
} while (i<10);
#include <stdio.h>
int main()
{
int num, index = 0;
printf("Enter a number : ");
scanf("%d", &num);
do
{
printf("%d\n", index + 1);
index = index + 1;
} while (index < num);
return 0;
}
While loop is called a pre-tested loop. The while loop allows code to be executed multiple times, depending upon a boolean condition that is given as a test expression. While studying for loop, we have seen that the number of iterations is known, whereas while loops are used in situations where we do not know the exact number of iterations of the loop. The while loop execution is terminated on the basis of the Boolean (true or false) test condition.
// structure
while (condition)
{
// code to be executed
}
// code
int i = 0;
while (i < 30)
{
printf("%d\n", i);
i = i + 1;
}
The "For" Loop is used to repeat a specific code until a specific condition is satisfied. The for-loop statement is very specialized. We use for a loop when we know the number of iterations we want, whereas when we do not know about the number of iterations, we use while loop.
//stucture
for ( initialize counter ; test counter ; increment/decrement counter)
{
//set of statements
}
//code
for(i=0;i<5;i++){
printf("%d");
}
An infinite loop also known as an endless loop occurs when a condition always evaluates to true. Usually,
this is considered an error.
Sometimes, while executing a loop, it becomes necessary to jump
out of the loop. For this, we will
use the break statement or continue statement.
#include <stdio.h>
int main()
{
int i, age;
for (i = 0; i < 10; i++)
{
printf("%d\nEnter your age : ", i);
scanf("%d", &age);
if (age > 10)
{
break;
// but in case of nested loop, only one is terminated, the outer loop keeps on looping
}
}
return 0;
}
#include <stdio.h>
int main()
{
int i, age;
for (i = 0; i < 10; i++)
{
printf("%d\nEnter your age : ", i);
scanf("%d", &age);
if (age > 10)
{
break;
}
else if (age < 0)
{
break;
}
}
return 0;
}