C Loops

In C programming loops are used to execute a set of statements until a specific condition is true. That means if the condition gets false then will be terminated.

There are two types of loops :

Entry control loop:

Entry control loop, the conditional is checked then the program is executed.
There are two types of entry control loop.

  • for loop.
  • while loop

Exit control loop

Exit control loop, the program is executed first and then the condition is checked. There are only one type loop in exit control loop.

  • do while loop.

while loop in C 

The while loop calculates the expression before every loop that is it checks for the condition to be true or false. If the expression is true then the block of statements inside while loop is executed, and if it is false, the statement will not be executed.
Increment or decrement of the expression is done inside the loop.
Its the ability to check the condition at the start makes it entry-control loop.


do while loop in C 

This is equivalent to a while loop, but it has test condition at the end of the loop. It is an exit-control loop. The Do while loop will always execute at least once during the execution of a program.


for loop in C

It is an entry-control loop. For loops work like the corresponding while loop that is it will execute a block of the statement until the specified condition returns false.
It has three computing steps:

  • The first is the initialization of the counter variable.
  • The second expression is test or condition check for true or false, which is evaluated to see if the body of the loop should be executed or not.
  • The third expression is increment or decrement of countered variable which is performed at the end of every iteration/repetition of the loop.


Nested Loop in C

Nested loops mean one loop inside another loop. The following example helps to explain.

Syntax of nested for loop

Syntax of nested while loop

Syntax of nested do while loop

Infinite Loop

A loop that repeats indefinitely and never terminates. These are called Infinite Loop. These loops occur infinitely because their condition is always true. We can make an infinite loop by leaving its conditional expression empty (this is one of the many possible ways). When the conditional expression is empty, it is assumed to be true.