C Loops

In programming, loops are piece of code which allows a block of statement to run multiple times until a certain condition is met. A loop consists of two parts, a body and a control statement. The body is the block of code which have to be executed and the control statement is the condition. Loops have numerous applications like printing a list of numbers, constantly checking the heart rate of a patient, displaying time in a digital clock or checking the internet connection while you are playing the T-Rex Game in your chrome browser.

The for Loop

The for loop consists of the three parts; initialize, condition, and increment/decrement. The initialize section allows setting a variable with a initial value. The condition section checks for a particular condition. If the condition is true, then only the loop will execute. Finally the increment/decrement section allows incrementing and decrementing of a variable (generally, the variable that has been initialized in the initialize section). Further, the increment/decrement can be performed with both pre/post increment/decrement and hardly makes any difference with respect to todays compilers. The pre increment can be slightly faster as there is no temporary variable involved in storing the past value.

Syntax

for(initialize;condition;increment/decrement)   //Control
{
   //Body
}

Code

#include<stdio.h>
int main(void)
{
    int i,j;
    
    printf("Natural numbers from 1 to 10\n");
    for(i = 1; i <= 10 ; i++)
        printf("%d\n",i);
    
    //Multiple variable for loop
    printf("Odd numbers from 1 to 10 and 101 to 110 side by side\n");
    for(i = 1, j = 101; i <= 10 ; i = i+2, j = j+2 )
    {
        printf("i = %d ",i);
        printf("j = %d\n",j);
    }
    
    return 0;
}

Output

Natural numbers from 1 to 10
1
2
3
4
5
6
7
8
9
10
Odd numbers from 1 to 10 and 101 to 110 side by side
i = 1 j = 101
i = 3 j = 103
i = 5 j = 105
i = 7 j = 107
i = 9 j = 109

The while Loop

The while loop is an entry control loop i.e. the condition is checked before the execution of the loop. The while loop repeatedly executes until the condition is not false.

Syntax

while(condition)
{
   //body
}

Code

#include<stdio.h>
int main(void)
{
    int i=10;
    
    printf("Natural numbers from 1 to 10 in reverse\n");
    
    while(i >= 1)
    {
        printf("%d\n",i);
        i--;
    }
    
    return 0;
}

Output

Natural numbers from 1 to 10 in reverse
10
9
8
7
6
5
4
3
2
1

The do while Loop

The do while loop is similar to while loop but has a slight difference. It is an exit control loop i.e. the condition is checked after the execution of the loop.

Syntax

do
{
   //body
}while(condition);

Code

#include<stdio.h>
int main(void)
{
    int i=10;
    
    printf("Natural numbers from 1 to 10 in reverse\n");
    
    do
    {
        printf("%d\n",i);
        i--;
    }while(i >= 1);
    
    return 0;
}

Output

Natural numbers from 1 to 10 in reverse
10
9
8
7
6
5
4
3
2
1

Infinite Loop

An infinite loop is caused when the condition in the control statement never gets false. Infinite loops are sometime intentionally created like in the case where input is taken from the user repeatedly until he or she quits. Infinite loops can be created in the following ways.

  • for loop
  • while loop
  • do while loop
  • goto statement
  • Recursion
  • Macros

The following code demonstrates various ways of creating an infinite loop

#include<stdio.h>

//Infinite loop using Macros
#define ITERATE \
for(;;){//body}

int main(void)
{
    //Infinite loop using for loop
    for(;;)
    {
      //body
    }
    
    //Infinite loop using while loop
    while(1)
    {
      //body
    }
    
    //Infinite loop using do while loop
    do
    {
      //body
    }while(1);
    
    //Infinite loop using recursion
    main();
    
    //Infinite loop using goto statement
    label:
     //body
    goto label;
    
    return 0;    
}

The break and continue statements in a loop

Sometimes we have to skip some iterations or stop a loop if a certain condition occurs. The break statement allows stopping of a loop and the continue statement allow skipping a particular iteration. The break statement only break execution of a single loop and not the outer loops if any.

Code

#include<stdio.h>
int main(void)
{
    int i;
	
    printf("Even numbers from 1 to 10\n");
    for(i = 1; i <= 100; i++)  //Looping till hundred
    {
        if(i % 2 == 1)  //skips odd numbers
    	    continue;	
    	
        printf("%d\n",i);
    	
        if(i == 10)  //breaks when i is equals 10
    	    break;		
    }
	
    return 0;
}

Output

Even numbers from 1 to 10
2
4
6
8
10

Trending

C Programming

Remember to follow community guidelines and don't go off topic.