Home >>C Tutorial >C Loop

C Loop

Loops in C

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.

Why loops are used in the C language?

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.

Let's understand the Advantage of loops in the C Language

  • Loops in C provides reusability of the code.
  • There is no need to write the same code repeatedly when using the loops.
  • Users can traverse over the elements of data structures (array or linked lists) by the uses of loops in C.

Have a look at the Types of the Loops in the language

There are generally 3 types of loops in the C language which are as follows:

  • Do While
  • While
  • For

Do-While loop in the C language

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);    

While loop in the C language

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  
}  

For loop in the C language

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  
}  

Popular Tutorials