Here is a guide for loops in C: how to make use of them and types of loops

Here is a guide for loops in C: how to make use of them and types of loops
Techiio-author
Written by Shuvhojit DebFebruary 16, 2022
8 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

We will be learning about loops in C programming in this blog post.We will learn how to use loops in C programming, their advantages, all the types of loops and a brief description of each type.

About loops in C language:

Looping can be defined as repeating the same process multiple times until a specific condition is satisfied. There are three types of loops used in the C language. In this part of the tutorial, we are going to learn all the aspects of C loops.

Uses of loops in C language:

The looping simplifies the complex problems into easy ones. It enables us to alter the flow of the program so that instead of writing the same code, again and again, we can repeat the same code a finite number of times. For example, if we need to print the first 10 natural numbers then, instead of using the print statement 10 times, we can print inside a loop that runs up to 10 iterations.

Advantages of loops in C:
  1. It provides code reusability.
  2. Using loops, we do not need to write the same code again and again.
  3. Using loops, we can traverse over the elements of data structures (array or linked lists).
Types of loops in C:
  • For loop
  • While loop
  • Do-While loop

1) For loop :

It executes the set of statements until the time a particular condition is accomplished. It is known as the Open-ended loop. In For loop, we can have more than one initialization or increment/decrement, separated using a comma operator and one condition as well. For loop is used to evaluate the initialization part first, and then it checks the condition for true or false. If the condition is true, then it executes the set of statements of for loop. After that, it evaluates the increment or decrement condition until the condition becomes false, it repeats the same steps. It will exit the loop when the condition is false.

Syntax:

for (initial value; condition; incrementation or decrementation )
{
statements;
}

Example-1:

#include<stdio.h>
#include<conio.h>
int main()
{
int number;
for(number=1;number<=5;number++)
{
printf("%d\n",number);
}
return 0;
}

Output:

blogpost

There are nested For loops as well in which there is the outer For loop and inner loop. In this nested loop, the inner loop is repeated for the times for a given condition of outer loop iteration.

Syntax:

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

Example-2:

#include<stdio.h>
#include<conio.h>
void main( )
{
int i, j;
for(i = 1; i < 5; i++)
{
printf("\n");
for(j = i; j > 0; j--)
{
printf("%d", j);
}
}
}

Output:

blogpost

2) While Loop:

In this, the condition is evaluated before processing the body of the loop. If the condition is true, then only the body of the while loop is executed. Then the control goes back to the beginning after completing the loop once. The statements in the while loop will be executed again, and if the condition is true and checked, then this process goes on until the condition becomes false. If the condition is false, the control will go out of the loop. After completion of the loop, the control will go to the statement that is immediately after the loop, and the body can contain more than one statement. The curly braces are not that important if it contains only one statement. If the condition is not true in the while loop, then loop statements won’t get executed.

Syntax:

while (condition) {
statements;
}

Example:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
while(num<=5)
{
printf("%d\n",num);
num++;
}
return 0;
}

Output:

It will print the numbers from 1 to 5 like below.

blogpost

3) Do While Loop:

In this loop, the statements in the loop need to be executed at least once. After that, it checks the condition. If the condition is true, then it will again have executed the loop; otherwise, it will exit the loop. It is known as an exit controlled loop. It is similar to a while loop, and in this, the condition is always executed after the body of the loop. The while loop is executed only when the condition is true, but sometimes the statement needs to be executed at least once, so for that do-while loop has to be used. The difference between while and do-while loop is that in while loop while is written in the beginning and in do-while, the condition is mentioned at the end, and it ends with a semicolon (;).

Syntax:

do {
statements
} while (expression);

Example:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
do
{
printf ("%d\n",2*num);
num++;
}
while(num<=5);
return 0;
}

Output:

The output of the above program is:

blogpost

C Language
Programming
Loops
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance