Home >>C Tutorial >C Loop
The loops in C language are generally defined as repeating the same process multiple times until a specific condition gets satisfied.
There are basically 3 types of loops that are used in the C language. In this tutorial, you’ll learn all the aspects of C loops.
The complex problems are simplified into the easy ones by looping. It generally enables the users to alter the flow of program and instead of writing the same code repeatedly, users can only repeat the same code only for a finite number of times. Let’s understand it with an example, in order to print the first 10 natural numbers, instead of using the printf statement repeatedly 10 times, users can print the first 10 natural numbers inside a loop that runs up to 10 iterations.
There are generally 3 types of loops in the C language which are as follows:
Until a given condition is satisfied, the do while loop in the C language continues to run. The do-while loop is also known as post tested loop. Whenever it is necessary to execute the loop at least once (mostly menu driven programs), the do-while loop is used.
Here is the syntax of the do-while loop in c language:
do { //code that is to be executed } while(condition);
The while loop in the C language is generally used when the number of iterations are not known in advance. Until the condition specified in the while loop is satisfied, the block of statements is executed in the while loop. This process is also known as a pre-tested loop.
Here is the syntax of while loop in the C language:
while(condition) { //code that is to be executed }
The for loop in the C language is generally used when there is a need to execute some part of the code until the pre specified condition is satisfied. The for loop is also known as a per-tested loop. If the number of iteration is known in advance then it is better to use the for loop.
Here is the syntax of the for loop in C language:
for(initialization;condition;incr/decr) { //code that is to be executed }