Why Loops are used in C?

For example, suppose you want to print the same words ten times. You can type ten printf functions, but it is more convenient to use a loop. All you need to do is to set up a loop that executes the same printf function ten times. We use the concept of looping when we need to execute the same action several times. When we write the code, again and again, we have to use the loop body. In this article, we will discuss why loops are used in C programming? and category of loop with appropriate example and explanation.

Table of Content
  1. Looping
  2. Advantage of Loop
  3. Categories of Loop
    1. Entry Control Loop
    2. Exit Control Loop

“Looping” means repeating a task

  • Looping is the act of repeating a set of instructions.
  • “Finite loops” refers to repeated executions of instructions in a certain number of iterations.
  • Infinite loops refer to the continuous execution of instructions.
  • This is the case with looping statements or iterations that execute repeatedly through a loop.

Why Loops are used in C?

  • Reduce the length of the code.
  • You will save time.

Category of Loops in C

  1. Entry Control Loop
    1. for Loop
    2. while Loop
  2. Exit Control Loop
    1. do – while Loop

Entry Control Loop

  • In the entry control loop, first, the condition will check then instructions or looping statements will execute.
  • A for and while loop is an entry control loop.

for Loop

  • Most programming languages use the for loop, which is the most popular and most used loop structure.
  • A for loop is an efficient way to write a loop that repeats a specific number of times.
  • When a for loop is used, it repeats a set of statements until a given condition is met.
  • If the user knows how many iterations there have been, that is good.
Syntax
for(Initialization; Condition; Increment/Decrement) 
{ 
  Body of the loop; 
  Iterations; 
}
TermDescription
InitializationThe initial value of the loop counter.
Condition If you specify how many iterations the loop will run, then it determines how many times the loop will run.
Increment / Decrement Amount of change in loop counter either increased or decreased (++, – -).

Working of the For Loop

Step 1:
  • We execute the initialization step first, only once.
  • During this step, you can declare and initialize any loop control variables.
Step 2:
  • Afterward, we have the condition test.
  • The loop executes if the condition is true, and if it is false, the body of the loop does not execute.
  • And then the flow of control jumps to the next statement just after the ‘for’ loop.
Step 3:
  • Then loop statement is execute.
  • Depending on the loop counter or loop variable value, it will increase or decrease.
Step 4:
  • Then, the condition is tested again and the same process as in step 2 is run until the condition goes away.
Program
Print 1 to 10 Number #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for(i=1; i<=10; i++) { printf("%d\n",i); } getch(); }
Output
1 2 3 4 5 6 7 8 9 10

while Loop

  • The while loop is an entry control loop.
  • As long as a given condition is true, repeat a statement or group of statements.
  • In order to execute the loop body, it first tests the condition.
  • You can use a while loop when you don’t know how many times you need to run a loop.
Syntax
Initialization; 
while(Condition) 
{ 
  Body of the Loop;  
  Increment/Decrement; 
}

Working of while Loop

Step 1:
  • The value will initialize to loop counter or loop variable.
Step 2:
  • The while loop will test the condition.
  • If the condition is true, then the loop will execute the statements.
Step 3:
  • Loop variable values will either increase or decrease.
  • Again condition will test and so on.
Step 4:
  • In a while loop, if the condition is false, the procedural statements that are after the loop body will execute.
Program
              Print 5 to 10 Numbers
#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
  int i; 
  clrscr();
  i=5; 
  while(i<=10) 
  { 
    printf("%d\n", i); 
    i++; 
  } 
  getch(); 
}
Output
5 
6 
7 
8 
9 
10

Exit Control Loop

  • If the condition is false, the exit control loop will execute at least one time.
  • Do – while is an exit control loop.

do – while Loop

  • In a do – while loop, the first-time body of the loop will execute then the condition will test means one time the statement will execute even the condition is false.
Syntax
Initialization; 
do 
{ 
  Body of the loop; 
  Increment/Decrement;
}while(Condition);

Working of do – while Loop

Step 1:
  • The loop variable or loop counter will initialize.
Step 2:
  • It will execute the body of the loop.
Step 3:
  • We will test the condition.
  • If condition is true, then again body of the loop will executed, otherwise the next procedural statements will executed.
Program
         Print 5 Table
#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
  int n,i; 
  clrscr();
  n=5; 
  i=1; 
  do 
  { 
    printf("%d\n",n*i); 
    i++; 
  } while(i<=10); 
 getch(); 
}
Output
5  
10 
15 
20 
25 
30 
35 
40 
45 
50

Conclusion

After reading this article, I hope you have a better understanding of what a loop is. Now you get to see the value of a loop in any language. In order to create a complex program, you can use a do-wile or a while loop. When you want a simple program, you can use a for loop. To know more about this topic, please click on the link below: