C – Loops4 min read

There may be times in a program where a block of code needs to be executed several times sequentially. In such cases, Loops are used to execute a sequence of statements many times until the stated condition becomes false.

Loops in C programming
Loops in C Programming

Two Types of Loops in C Programming.

1. Entry controlled loop:

In this type loop, the stated conditional is checked first and then the block of code is executed. It is also called a pre-checking loop.

There are two types of entry control loop:

  • for loop. 
  • while loop.

2. Exit controlled loop:

In this type loop, the block of code is executed first and then the condition is checked. It is also called a post-checking loop.

There is only one type loop in the exit control loop.

  • do-while loop.

While Loop in C

A while loop is a straight forward loop and is also an entry-control loop. It evaluates the condition before executing the block of the loop. If the condition is found to be true, only then the body of the loop is executed.

The loop continues until the condition stated found to be false.

While Loop in C

Syntax of While Loop in C.


2. do-while loop in C:

do-while loop in C is just as same as while loop, only difference is that in do-while the condition is always executed after the loop as shown in syntax below.

Unlike while loop, where the body of the loop is executed only if the condition stated is true but in the do-while loop, the body of the loop is executed at least once. do-while is an exit-control loop that is it checks the condition after the first execution of the block.

do-while loop in C

Syntax of do-while Loop in C.


3. for Loop in C

In C programming, for loop is a more efficient loop structure and is an entry-control loop. The iteration continues until the stated condition becomes false.

It has three computing steps as shown in the syntax below.

  • initialization: The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(;).
  • condition: Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(;).
  • Increment or Decrement: The third one is increment or decrement of the variable for the next iteration. Here, we need to use the semicolon(;).
for loop in C

Syntax of for loop in C


The Infinite Loop

As the name suggests, the infinite loop is a forever executing loop. The infinite loop repeats indefinitely and never terminates. It means the condition never turns to false. It is also known as an indefinite loop or an endless loop.

We can use one of the Loops in C to turn it into infinite loop.

infinite ‘for’ loop

Example:

If the above program is executed then we will get infinite printf statement. The compiler assume the condition to be true if the condition is absent in the loop.

NOTE: You can terminate an infinite loop by pressing Ctrl + C keys

We can also create infinite while loop by defining the condition as shown below.

infinite do-while loop


MORE

Find a pair with the Given Sum in an Array in Java

In this tutorial, we will write a java program to find the sum pair in an array. Before that, you may go through the following …
Read More

C++ Program to display Pascal Triangle

In this tutorial, we will write a C++ program to display pascal’s triangle. Before that, you may go through the following topic in C++. for …
Read More

C++ Program to Calculate Sum of Geometric Progression

In this tutorial, we will learn and write a program to find the sum of GP series in C++. Before that, you may go through …
Read More

C++ Program to find the Transpose of a Matrix

In this tutorial, we will learn and write a program to find the transpose of matric in C++. Before that, you may go through the …
Read More

How to Split a String in C++

Let us learn how to split string in C++ programming language. We will look at a few ways to split a string. The splitting of …
Read More

C++ Program to calculate Average of Numbers using Arrays

In this tutorial, we will learn and write a C++ program to find the average of numbers using an array. To understand the coding, you …
Read More