What is Loop Statement in C Programming?

Welcome Guys, till now we have seen a lot about this series, in this module, we are going to talk about what is loop statement in C Programming, like suppose you want to repeat a set of expressions many times then for that purpose C provides the concepts of loops in programming.

What is Loop Statement in C Programming?

So, let’s dive into the depth of this Loop Statement in C Programming concept.

What is Loop Statement in C Programming?

Loops are often called iteration statements in C programming i.e. if you want to repeat a particular statement or expression a particular number of times. It executes a statement multiple times until the condition results are false.

Loops statement in C programming consists of two parts i.e., a loop body and a control condition. The control condition consists of a set of statements that directs the loop body to be executed, i.e., the loop body will get executed until the condition results to be false.

Loop Statement in C programming

In simple, we can say that loops are used to repeat a particular block until some specified condition is met. C Programming provides us with three types of loops for meeting the looping requirements.

List of Loop Statement in C Programming

  1. While loop
  2. Do-while loop
  3. For loop

Let’s see Loop Statement in C Programming in detail.

What is While loop in C Programming?

While loop statement is a first loop statement in C programming. It is the simplest kind of loop and is more straightforward. It evaluates a certain condition. It first checks the condition and if it is true, then it allows the program to enter inside the loop body, and as the condition fails, the program comes out of the loop. The syntax for declaring the while loop in C Programming is:

while (expression)
{
  // statement to be executed or repeated
}

After getting out of the loop, the program executes the very first statement written outside the loop. The loop body can contain more than one statement to be repeated, as per the requirement by the programmer. If the loop body contains only one statement then there is no need for curly braces, however, if there is more than one statement in the loop body, then curly braces are important. Although it is a good practice to use curly braces while programming, that’s the work of the smart coder.

Let’s see one example demonstrating a while loop statement in C programming.

#include <stdio.h>
int main( )
{ 
int n = 15;
while (n>0) {
printf("%d ",n);
n = n - 1;
}
printf("Loop body finished");
return 0;
}
15 14 13 12 10 9 8 7 6 5 4 3 2 1 Loop body finished

In the above program ‘while loop statement in C programming’, we initialized the variable n to value 15, then we used a while loop and the condition is that the value of n should be greater than 0, and in the loop body we printed the value of n and then decrementing the value of n by 1, so this case the loop body will keep executing until or unless the value of n becomes less than 1. Hence, we got the output.

What is a do-while loop in C Programming?

Do While loop statement is a second loop statement in C programming. It is very similar to the while loop, the only difference is that the condition is checked at the last. The body of the loop is executed at least once if the condition gets never fulfilled also because the condition is declared at the last of the loop body. The syntax for declaring the same is.

do
{
// statement to be executed or repeated 
}
while (condition);

Notice that the condition is at the last with the while statement.

#include <stdio.h>
int main()
{ 
int n;
do {
printf("Enter value: ");
scanf("%d", &n);
printf("The value is: %d\n",n);
} while (n != 20);
printf("do-while loop body finished");
return 0;
}

The output of the above code is:

Enter value: 2
The value is: 2
Enter value: 34
The value is: 34
Enter value: 20
do-while loop body finished

In the above program ‘Do While loop statement in C programming‘, inside the do block, we have performed several actions like taking the input and displaying the same, whereas the condition given is that the value entered should not be equal to 20, as the user enters the value 20, the program will come out of the loop.

What is for loop in C Programming?

A loop statement is a third loop statement in C programming. It is the most used loop in C Programming, and is more efficient also, this loop is designed to iterate a statement a number of times. Like while loop, for loops, also checks the condition first and then allow executing a certain block. The syntax for declaring the for loop is:

for (initialization; condition; increment/decrement)
{  
// statement to be executed
}

It works in the following ways:

  • Initialization is performed only once in the for a loop.
  • The condition is the set of expressions that tests and compares to a fixed value for each iteration. It stops the iterations if the condition results are false.
  • The increment or decrement changes the condition value as per required.

Let’s see some examples of the for loop statement in C programming to get the concept clearer.

#include <stdio.h>
int main()
{   
    int i;
    for (i=0; i<10; i++)
    { 
        printf("%d ", i);
    }
    printf("for loop body finished");
    return 0;
}

The output of the above code is:

0 1 2 3 4 5 6 7 8 9 for loop body finished

In the above code ‘for loop statement in C programming’, we are printing the value 0-9 with the help of for loop, firstly we have initialized the value to 0, and then we have given the condition that the value should be less than 10 and increment the value. Hence, we got the output.

What is the difference between entry controlled loop and exit controlled loop in c programming

We have talked about three types of loops till now, so these loops lie under two categories and which are entry-controlled and exit-controlled loops. So, let’s see the difference between both to get clearer.

Entry Controlled loop Exit controlled loop
In this loop, the test condition is checked first before executing the loop body. If the condition results are true then it will enter the loop body. In this loop, the loop body is executed first before checking the condition.
The loop body will not be executed if the condition results are false. At least once, the body of the loop will get executed, irrespective of the condition.
For loop and while loop comes under the entry-controlled loop. Do while loop comes under the exit-controlled loop.
These loops are used when checking of test condition is mandatory before executing the loop body. These loops are used when checking of test condition is mandatory after execution.

I hope you all enjoyed learning this module (What is Loop Statement in C Programming), it is the most exciting topic of the series, Stay connected for upcoming modules.

Keep Learning, Keep Practicing, and Happy Coding!