Loops (while, do-while, for)
In C programming language, loops are used to repeatedly execute a block of a particular program code in the program. Until a specified program condition given in the current program is fully executed. There are mainly 3 types of loops in C programming. Which are used by the C developer based on their need. Such as, while loop, do-while loop, and for loops, etc.
while Loop in C language.
In C language, the while loop repeats a block of program code. Until the given program condition is evaluated as true up to a specified limit. In the while loop, the program condition is first given. Then it is repeated up to a certain limit. Until the given while condition is fully executed.
while Loop Syntax.
while (program condition
{
// Code to be executed when condition execute
}
while Loop program Example.
int p = 1;
while (p < 7)
{
printf(“\n %d”, p);
p++;
}
do-while Loop in c language.
do-while loop is similar to while loop in c language. But do-while loop executes a particular block of program code at least once before checking the program condition. Whereas in do-while loop first program statement is printed or initialized. Then in while loop the condition is tested.
do-while Loop Syntax in c programming.
do {
// program Code to be executed
} while (program condition);
do-while Loop program Example in c.
int q = 1;
do {
printf(“\n%d”, q);
q++;
} while (q < 7);
for Loop in c programming.
for loop in C Programming Program Loop provides a loop concept to iterate over a certain range of values. for loop mainly consists of three parts. For loop initialization, program condition, and increment/decrement. Here in for loop, condition is initialized, condition is checked, and for loop is incremented or decremented according to the condition.
for Loop Syntax in C Programming.
for (initialization; condition; increment/decrement)
{
// program Code condition to be executed
}
for Loop Example in C Programming.
for (int p = 0; p <=7 ; p++)
{
printf(“\n %d”, p);
}
about while loop, do-while loop, and for loops in c.
Use the while loop in C language when you want to repeatedly execute a block of program code based on a given program condition evaluated before each program iteration.
The do-while loop in C language is used when you want to execute a block of program code at least once in the current program regardless of the given program condition.
The most common and convenient use of the for loop in C language is when you already know the number of loop repetitions in your current program, or when you need to control the iteration process more explicitly. The for loop executes the program from start to end.