Loop control in C

Introduction to loops


While writing a program in C when we need to perform a single task a number of times instead of typing the same exact line/s over and over again we can type them inside a loop and they would get executed as many times as we want them to .

you have provided loops in C language. With the help of loops, you can execute the same statement repeatedly. Each type of loop provides a block in which statements are written which you want to execute more than once.

Loop is made up of 3 things.

  • Initial variable:  This is the variable where you start the loop. This is an integer variable. This variable is incremented until the given condition becomes false. this variable is included in the condition of the loop.
  • Condition: This is the condition that controls the loop. As long as the condition remains true, the loop keeps execute. As soon as this condition is false the loops gets terminate.
  • Increment: how much you want to increment from or how to initial variable / condition is defined in this increment part.

Types of loop:

There are 3 types of loops these are :

  1. While loop
  2. Do while loop
  3. For loop

While loop

In a while we have a condition and as long that condition is being satisfied the loop will keep running.

  • while loop is a simple loop, it executes as long as the condition remains true.
  • when the condition is false, this loop gets terminated.

This general syntax of while loop is being given below.

initial variable declaration;
while (condition)
{
     //statements;
   //increment;
}

Let’s see an example to understand how it really works.

#include<stdio.h>
void main()
{
    int a = 1;
    while(a<=5)        //loop will run as long as a is less or equal to 5
    {
        printf("loop ran\n");     //the \n will make the next output to be printed in next line
        a++;            //value of a increases by one
    }
}

Output :

loop ran
loop ran
loop ran
loop ran
loop ran

Do while

Do while loop is exactly like while loop the only difference being in do while loop the loop will run the 1st time even if the condition is not satisfied and it checks the condition from the 2nd time and onwards .

The general syntax of Do while loop is given below:

do
{
     //statements;
   // increment;
}while(condition);

Example

#include<stdio.h>
void main()
{
   int a = 10;
   do
   {
       printf("loop ran\n");       //the \n will make the next output to be printed in next line
       a++;           //value of a increases by one
   }while(a<=5)        //loop will run once an then as long as a is less or equal to 5
}

Output :

loop ran

For loop

  • In C loops for loop is mostly used.
  • This loop is very easy and gets define in a single statement.

For loop is like a while loop but in a for loop we tell the loop the new initialization (if any) ,the condition/s and the increment/decrements (after the loop runs) each separated by a semicolon .

The general syntax given below:

for(initial variable;condition;increment;)
{
      //statements;
}

Example:

#include>stdio.h>
void main()
{
    for(int a = 1; a <= 5;a++) //loop will run as long as a is less or equal to 5
    {
        printf("loop ran\n"); //the \n will make the next output to be printed in next line
    }
}

Output:

loop ran 
loop ran 
loop ran 
loop ran 
loop ran

Check the difference for the same output code in while and for loops.

Loop control

Sometimes for special reasons we need more control on your loop and these are provided by loop control statements.

Loop control statements are statements that change the execution of the loop according to any condition. theses statements are very useful for changing the loop accordingly.

These are also of three types of loop control statements

  1. break

  2. continue

  3. goto

Break Statement:

The break statement is used to terminate the loop. when a break statement is executed inside a loop, the loop get terminated at the same time and the execution of next statement begins after that loop.

In a while we have a condition and as long that condition is being satisfied the loop will keep running.

here’s the syntax is:

break;

Let’s see an example to understand how it really works.

#include<stdio.h>
void main()
{
   for(int i=1;i<=5;i++)
   for(int j=1;i<=5;j++)
   printf("%d %d \n",i,j);      //prints value of i and j in that iteration of the loop
   if(j==3 || i==3)
   {
      break;
   }
}

Output :

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Continue Statement:

it is used to skip any iteration of the continue statement loop on the C language.

for example, if you want the third iteration of the loop not to be executed, then you can skip it by continuing the statement by detecting the third iteration by condition.

here’s the syntax:

continue;

Example:

#include<stdio.h>
void main()
{
           int i;
          for(i=1;i<=5;i++)
          {
                 if(i==3)
                 {
                      printf(" third iteration skipped");         // skipping third iteration using continue statement
                      continue;
                 }
                 printf("%d",i);
           }
}

Output:

1
2
third  iteration skipped
4
5

Goto Statement:

At last with the goto statement we can go back to a previous step or jump some steps coming up by putting a label followed by a colon and then compiler will start executing the program from there again.

here’s the syntax:

...
...
goto label-name
...
label-name
...
...

Example:

#include<stdio.h>
void main()
{
   int age;
   lab1:
   printf("You are not eligible to vote\n");
   printf(Enter your age\n");
   scanf("%d",&age);
   if(age<18)
      goto lab1;
   else
      printf("You are eligible to vote\n");
}

Input :

16
14
21

Output:

You are not eligible to vote
You are not eligible to vote
You are eligible to vote

5 comments on “Loop control in C”


  • Bhavani

    The actual code is :
    #include
    void main()
    {
    for(int i=1;i<=5;i++){
    for(int j=1;i<=5;j++){
    printf("%d %d \n",i,j); //prints value of i and j in that iteration of the
    loop
    if(j==3 || i==3)
    {
    break;
    }
    }
    }
    }
    And then the output is
    1 1
    1 2
    1 3
    2 1
    2 2
    2 3
    3 1
    4 1
    4 2
    4 3
    5 1
    5 2
    5 3
    Whenever the break keyword encounter by compiler and it is inside loop it exits from whole that's y it just stop of 13,23….


  • Angel

    #include
    void main()
    {
    for(int i=1;i<=5;i++)
    for(int j=1;i<=5;j++)
    printf("%d %d \n",i,j); //prints value of i and j in that iteration of the loop
    if(j==3 || i==3)
    {
    break;
    }
    }
    Plz explain this??


    • Bhavani

      The actual code is :
      #include
      void main()
      {
      for(int i=1;i<=5;i++){
      for(int j=1;i<=5;j++){
      printf("%d %d \n",i,j); //prints value of i and j in that iteration of the
      loop
      if(j==3 || i==3)
      {
      break;
      }
      }
      }
      }
      And then the output is
      1 1
      1 2
      1 3
      2 1
      2 2
      2 3
      3 1
      4 1
      4 2
      4 3
      5 1
      5 2
      5 3
      Whenever the break keyword encounter by compiler and it is inside loop it exits from whole that's y it just stop of 13,23….