Looping statement in C programming

The statement that is used to repeat the programming instructions again and again until a defined condition is satisfied is known as looping statement. The statements are executed sequentially where the first statement is executed first, followed by the second and so on.

There are four types of looping statements which are explained below.

1. for loop 

For loop is used to repeat a group of statements that needs to be executed a specific number of times.

Syntax:

for(init; condition; increment/decrement)
{
  statement;
}

The init step is executed first and only once. It allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the for loop terminates.

After the body of the for loop executes, the flow of control jumps back up to the increment/decrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself. After the condition becomes false, the for loop terminates.

Example : 

Print numbers from 1 to 100

#include<stdio.h>
int main()
{
  int i;
  printf("The number from 1 to 100 are \n");
  for(i=1; i<=100; i++)
  {
    printf("%d\n",i);
  }
}

2. while loop

Whle loop is also called pre-test loop or entry control loop. When the number of repetition of a loop is not known in advance, then it is better to use while loop.

Syntax:

i=1;
while (i<=10)
{
statement;
increment/decrement;
}

Example : 

Print numbers from 1 to 20.

#include<stdio.h>
int main () {
    
   int a = 1;
   
   while( a <= 20 ) {
      printf("%d\n", a);
      a++;
   }

   return 0;
}

Read a number and find the sum of all digits of the number 

#include<stdio.h>
int main () {
    
  int n, sum = 0, r;
  printf("Enter the number");
  scanf("%d",&n);
  while (n != 0)
  {
    r = n%10;
    sum=sum+r;
    n=n/10;
  }
  printf ("The sum of all digits of the given number is %d",sum);
}

3. do while loop

do while loop is also called exit controlled looop or post test loop. When the test condition is true for at least once, we can use the do while loop. It enters into the loop at least once and checks the test condition. As long as the test condition is true the loop operation will be repeated. The do while loop will be executed at least once.

The difference between the while loop and do while loop is that in while loop, the condition is tested at the beginning of the loop whereas in case of do while loop the condition is tested at the end of loop.

Syntax:

do 
{
   statement;
} while (condition);

Example: 

Generate the multiplication table of n.

#include <stdio.h>
int main()
{
  int n,i,m;
  printf("Enter a number to generate it's multiplication table \n");
  scanf("%d",&n);
  i = 1;
  do
  {
    m = n*i;
    printf("%d * %d = %d\n",n,i,m);
    i++;
  }while (i<=10);
}

Nested loop

If one loop is defined inside another loop then it is known as nested loop.

Syntax :

for (expression 1a; expression1b; expression 1c;)
{
  for (expression2a; expression2b;expression2c)
    { 
      statement;
    }
  statement;

Example:

Print
111
222
333

#include<stdio.h>
int main()
{
  int a,b;
  for(a=1;a<=3;a++)
  {
    for(b=1;b<=3;b++)
    {
      printf("%d",a);
    }
    printf("\n");
  }
}

Loop Control Statements

1. Break statement

Break statement is used to terminate the loop or block in which it is defined. When the compiler encounters a break statement within the loop then the loop will be terminated and control goes to the first statement after closing braces of loop. Generally, it is used within the loop with ‘if statement’.

Example : 

Print the first 50 even numbers.

#include<stdio.h>
int main()
{
  int i,count=0;
  printf("The first 50 even numbers are \n");
  for( i =1; ; i++)
  {
    if (i%2==0)
    {
      printf("%d\n",i);
      count = count+1;
    }
    if (count==50)
    break;
  }
}

2. Continue statement

The cntinue statement is used to perform the reverse operation of break statement. When continue statement is encountered inside the loop the control goes to the beginning of the loop i.e. next iteration of the loop will start and remaining part of the loop will be skipped. Generally, it is used within the loop with ‘if statement’.

Example:

Get only odd numbers from 1 to 20.

#include<stdio.h>
int main()
{
    int i,b;
  for(i =1;i<=20;i++)
  {
    if (i%2==0)
    continue;
    printf("%d\n",i);
  }
}

3. Goto statement

Goto statement is used to control the flow of execution of program by altering the sequence of execution of program. It is used to transfer the control from one part of the program to other part of the program. The goto statement may be conditional goto or unconditional goto statement.

Conditional goto statement transfers the control from one part of the program to another part of program under certain condition. 

Syntax:

if (condition)
Goto label;

Unconditional goto statement is used to tansfer the control from one part of the program to other part of the program without any condition . Unconditional goto statement is not used by good programmer.

void main()
{
begin:
  printf("hello");
  goto begin;
}

Example :

#include <stdio.h>
int main()
{
  int a = 1;

   BEGIN:do {
      if( a > 7 && a < 15) {
         a = a + 1;
         goto BEGIN;
      }
      printf("value of a: %d\n", a);
      a++;
   }while( a < 20 );
   return 0;
}

0 Like 0 Dislike 0 Comment Share

Leave a comment