C Loops

In this tutorial, you will learn what is a loop and the different types of loops available in the C programming language with the help of examples.


While making any programs situation may arise where some statement or a group of statements need to be executed multiple times. In this situation, the loop statement comes into the pictures. In the C programming language, code statements are executed sequentially one after another; the first statement will execute first, followed by the second, and so on.

A loop statement executes a statement or group of statements multiple times until a specified condition is met. Following is the general structure of the loop statement in most of the programming languages.

c loops

Types of C Loop

The C programming language provides the following types of loops to fulfill the looping requirement.

  1. For Loop
  2. While Loop
  3. Do While Loop

For Loop

The for loop executes a statement or a group of statements repeatedly until the given condition is satisfied. It is better to use for loop when the numbers iteration are known in advance.

The syntax of for loop in the C programming language is as follows:

for (initialization of base case expr; test condition expr; increment/decrement expr)
{ 
// body of the loop or the block with the statements to execute
// statements will execute until condition is false
}

While Loop

The while loop executes a statement or a group of statements repeatedly until the given condition in the while loop is satisfied. It is used in the scenario where the number of iterations is not known in advance. The while loop is also called the pre-test loop.

The syntax of while loop in the C programming language is as follows:

Initialization of the expression
while (test or condition expression)
{
// statement which will be executed when the condition is true
update_expression;
}

Do While Loop

The do-while loop in C language continues to execute a statement or a group of statements repeatedly until the given condition satisfies. It is used in the scenario where the block of statements needs to be executed at least once. This is also called the post-test loop as the condition is checked only after the execution of the block of codes executed once.

The syntax of the do-while loop in the C programming language is as follows:

Initialization of the expression
do
{
// loop body which will be executed at least once.
update_expression;
}a
while (test or condition expression);

Nested Loop

When one or more loops are used in any other loop like for loop, while loop and do-while loop is called nested loop.

The syntax of the nested for loop in the C programming language is as follows:

for ( init; condition; increment ) {

for ( init; condition; increment ) {
statement(s);
}
statement(s);
}

The Infinite Loop

The infinite loop runs forever. When the condition never becomes false, the loop becomes an infinite loop. Generally, the for loop is used for this purpose. None of the three expressions that form the ‘for’ loop are required. So, you can make an infinite loop by leaving the conditional expression empty.

#include <stdio.h>

int main () {

for( ; ; ) {
printf("This line will print forever.\n");
}

return 0;
}

Please get connected & share!

Advertisement