×

C - LOOPS


In this chapter we will understand about looping in c.

What are loops in C :

Loops are used when we need to perform some task many number of times in loop until some condition is satisfied. Each statement inside a loop is execute sequentially.

Types of loops in C

for loop

while loop

do...while loop

Let's understand each type of loops one by one

for loop

for loop is a control structure in c which allows us to execute block of statement a specific number of times.

Syntax of for loop
for(initialization; condition expression; update statement)
{
    // code statements
}
                    

Explanation of For Loop Syntax :

To explain the syntax lets look at an example based on the above statment :

/*for(initialization; condition expression; update statement) */
int i;
for(i = 0; i < 5; i++)
{
    printf("i = %d", i); //statement
    printf("\n"); //statement
}  
  • initialization (i = 0):

    Here initialization of conditional variable (i.e. i) is done.The initialization statement is executed only once.

  • condition expression (i < 5):

    This is evaluated after the initialization statement. If the condition(i.e i < 5) is false, the loop terminates.However if the condition is true, the code statements inside the loop body is executed.

  • update statement (i++):

    Once all the statement inside the for loop is executed, this is updated. This process is called as iteration in loops. The number of times the iterations are performed, those many number of times the code inside loop body is executed. This process continues to repeat until the condition expression is false.

For loop is used when the number of iterations are known.

The conditional expression is true or false based on relational and logical operators.

For loop example
#include <stdio.h>
int main() {
  int number, iteration;
  printf("Enter a number > 0\n");
  scanf("%d", &number);

  for (int iteration = 0; iteration < number; iteration++) {
    printf("Iteration : %d\n", iteration);
  }

  return 0;
}
                    
Output
Enter a number > 0
5
Iteration : 0
Iteration : 1
Iteration : 2
Iteration : 3
Iteration : 4

while Loop

While loops is used when the number of iterations are not known, unlike for loop where the number of iterations are known in advance. Below is the syntax of while loop -

Syntax
while(condition)
{
    //statements
}
            

Here the statements will be iterating while the condition is true. This means while loop breaks when the condition is false. For Example

while_loop example
int i = 0;
while(i <= 10)
{
    printf("i = %d", i);
    i++; // if you remove this statement this loop will never end
}
            

Here while loop will break when the value of i > 10. Yes when i > 10 the loop will end.

do...while loop

do...while loop is same as while loop, except that do...while loop will execute at least once even if the condition is not true.

do...while syntax
do
{
    //statements
}while(condition);
            

Here the statements will execute at least once irrespective of the condition.

Example: do...while loop
int i=10;
do
{
    printf("i = %d", i);
}while(i<10);
            

Here do...while loop will print i = 10 in output even though i != 10.


Infinite loops

Whenever the loop enters in a stage where it never ends, it is called as infinite loop. For example, if you have a condition that says execute the loop until the value of a variable reaches to 1000, and that variable never reaches 1000, the loop will go into infinite stage where it will never end.

Sometimes Infinite loops are useful, but if used carelessly it can result in system hang and other issues.

Syntax of Infinite loop in C

for loop
for(;;)
{
    //statements
}
            
while loop
while(1)
{
    //statements
}
            
do loop
do
{
    //statements
}while(1);
            

Example of Infinite loop in C

for loop
for(;;)
{
    printf("Infinite for loop");
}
            
while loop
while(1)
{
    printf("Infinite while loop");
}
            
do loop
do
{
    printf("Infinite do...while loop");
}while(1);
            

In the next chapter, We will learn Arrays in C.