What is loop in C?

Safalta Expert Published by: Aryan Rana Updated Sun, 18 Sep 2022 10:59 PM IST

Highlights

The loop is used to repeatedly run the block of code in accordance with the condition stated in the loop. It entails repeated execution of the same code, which helps to traverse an array's items while also saving code.

Table of Content

1. What do loops in C mean?
2. Control statements for loops




 

What do loops in C mean?


The loop is used to execute the code block several times based on the condition stated in the loop. It entails repeating the same code execution, which helps to save code and navigate through an array's contents.  If you are interested in Digital Marketing or Graphic Designing and want to learn these interesting courses then click on the links mentioned Digital Marketing Course and Graphic Designing course

Download these FREE Ebooks:
1. Introduction to Digital Marketing
2. Website Planning and Creation



There are 3 types of loops –

while loop
do – while loop
for loop


1. while loop -

Until the condition is false, the while loop executes the code.

while(condition){
//code
}

e.g.

#include
#include
void main()
{
int i = 20;
while( i <=20 ) {
printf ("%d " , i );
i++;
}
getch();
}


Output

20
 

2. do - while loop


Additionally, the code is run until the condition is false. Whether the condition is true or not, code is performed at least once in this but not in while.

Free Demo Classes

Register here for Free Demo Classes

Please fill the name
Please enter only 10 digit mobile number
Please select course
Please fill the email
Only when the condition is met does the while loop come into play.

Syntax

do{
//code
}while(condition);

e.g.

#include
#include
void main()
{
int i = 20;
do{
printf ("%d " , i );
i++;
}
while( i < =20 );
getch();
}

Output

20
21


3. for loop


Additionally, the code is run until the condition is false. Three parameters are presented in this, namely
  • Initialization
  • Condition
  • Increment/Decrement

Syntax

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

When the number of iterations is known, it is used, however when the number of iterations is unknown, it is used.

e.g.

#include
#include
void main()
{
int i;
for( i = 20; i < 25; i++) {
printf ("%d " , i);
}
getch();
}


Output

20
21
22
23
24
 

Control statements for loops:


The execution of the loop is modified from how it usually operates by these control statements. These are the loop control structures:

1. The break statement is used to terminate a loop or switch statement and to move control to the statement that comes next.
2. continue statement - It skips a few statements based on the condition that is specified.
3. goto statement - This command gives the labelled statement control.






 

Why is C programming using loops?

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. The array and linked list are two common data structures that employ it. represents how the loop variable was set up. A variable may be initialised more than once.

How does loop syntax work?

In the C programming language, a for loop is written as for (init; condition; increment) statement(s); Here is an example of a "for" loop's control flow: The first and only execution of the init step occurs. Any loop control variables can be declared and initialised at this phase.

Free E Books