Loops in C Programming

Iterative statements

These types of statements are used for the repeat execution of a list of statements depending on the value of an integer expression.

The types of iterative statements are as follows:

1. While loop

  • This loop has a mechanism of repeating one or more statements while a particular condition is true.
  • In this type of loop, the condition is generally executed before any of the statements are executed.
  • If the condition is found to be true then only will the statements execute otherwise the condition will be false.
  • While loop will keep on executing as long as the condition is true.
Syntax:

statement x;
while (condition)
{
   statement block;
}
statement y;


while loop

Example: Write a program to calculate the sum of first 20 numbers.

#include<stdio.h>
void main()
{
        int x=0, sum=0;
        while (x<=20)
        {
              sum=sum+x;
              x=x+1;
        }
        printf("Sum of the first 20 numbers = %d", sum);
}


Output:
Sum of the first 20 numbers = 210

2. do-while loop

  • It is similar to the while loop.
  • The only difference is that the test condition here is ended at the end of the loop.
  • From this, it is clear that the body of the loop will be executed atleast once.
Syntax:

statement x;
do
{
     statement block;
}
while (condition);
statement y;


Note: The do-while loop should always have a semi-colon at the end.

do while loop

Example: Write a program to calculate the average of the first 10 numbers.

#include <stdio.h>
void main()
{
    int a=0, sum = 0;
    float avg;
    do
    {
        sum=sum+a;
        a=a+1;
    }
    while (a<=10);
        avg = sum/10.0;
    printf("The sum of the first 10 numbers are = %d",sum);
    printf("\n The average of the first 10 numbers are = %f",avg);
}


Output:
The sum of the first 10 numbers are = 55
The average of the first 10 numbers are = 5.000000

3. For loop

  • This loop is similar to the while and do-while loop.
  • It is a determinate or definite loop, as the programmer is aware as to how many times does he want the loop to be repeated.
  • This loop is initialized only once. When the loop is run, with every iteration the value of the loop is updated and the condition is checked.
  • If the condition is true the loop will be executed else the statements are skipped.
Syntax:

for (initialization; condition; increment/decrement)
{
     statement block;
}
statement y;


for loop

Example : Write a simple for loop program to print the first 10 numbers

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


Output:
1
2
3
4
5

Nested Loops

  • The loops inside the loop are called Nested loops. The most popular types of nested loops are 'for' loops as they are very easy to control.
  • 'For' loop can be used to control the number of times any statement needs to be executed. An outer loop is used for controlling the number of time the loop should be repeated.
  • They should be indented properly so that the reader can easily determine what statements are written within each of them.

Example

Write a program to print the following pattern:
*
* *
* * *

#include <stdio.h>
void main()
{
     int i,j;
     for (i=1; i<=3; i++)
     {
          for(j=1; j<=i; j++)
          printf("*");
          printf("\n");
     }
}


Loop Control Statements

Break Statement

  • The break statement terminates the execution of the nearest enclosing loop in which it appears.
  • Whenever the compiler comes across the break statement, the control is passed to the statement that follows the loop in which this statement appears.
  • The break statement is used for exiting a loop from the point within its body by passing a normal termination expression.
Syntax:
break;
break-statement
Fig: break Statement

Continue statement

  • This statement appears only in the body of the loop.
  • Whenever the compiler encounters this statement the rest of the statements are skipped and control is unconditionally transferred to the loop continuing portion of the nearest enclosing loop.
Syntax:
continue;
  • It is the opposite of break statement.
  • The next iteration of the loop is forced by skipping any code in between itself and the test condition of the loop. It is used for restarting a statement sequence whenever an error occurs.

Goto Statement

By using the goto statement, control can be transferred to any part of the program.

Syntax:
goto label;
  • The label should be there in the same function and can appear only one statement before the same function.
  • Whenever the goto statement is encountered the control gets transferred immediately to the statements following the label. It breaks the normal sequence of the execution.