C Programming

C Programming loop examples

Loop is a very essential part of any programming language to solve any problem. Three types of loops exist in most of the programming languages, just the declaration syntax is different for different languages. when we need to iteration some statements multiple times then a loop is used to do the tasks.  Like other programming languages, C language contains for, while and do-while loops. This article shows the declaration and the uses of these three loops in C language using multiple examples to clarify the purpose of using loop in programming.

For Loop:

This loop is better to use when the number of iterations is pre-defined. This loop contains three parts. The first part is used to set initialization variable from where the loop will start, the second part is used to set termination condition that defines the number of times the loop will iterate and the third part is used to increment or decrement the initialization variable for terminating the loop.

Syntax:

for (initialization; condition; increment/decrement)
{
  statements
}

Example-1: for loop with a single condition

The conditions can be applied in different ways in for loop. For loop can contain single condition, multiple conditions, and no condition. The following example shows the use of for loop with a single condition. An array of floating number is iterated here by using for loop. The loop will iterate 5 times and print the value of each array element.

#include<stdio.h>
int main()
{
  //Declare an array of float numbers
  float price[6] = { 870.45, 345.90, 209.45, 200.45, 543.67, 450.69 };

  // Print each element of the array using for loop
  for(int n=0 ;n<=5;n++)
  {
    printf("%.2f\n",price[n]);
  }

  return 0;
}
sysads@linuxhint $ gcc 1.c -o 1
sysads@linuxhint $ ./1
870.45
345.90
209.45
200.45
543.67
450.69

Example-2: for loop with multiple conditions

The following example shows the use of for loop with multiple conditions. Two initialization variables, x, and y are used in the loop. There are two conditions are used with OR logic as a termination condition. When the value of x will greater than 30 or the value of y will less than 5 then the loop will terminate otherwise it will print the value of x and y.

#include <stdio.h>
int main()
{
  // Declare variables for intialization
  int x, y;

  // Print the values of x and y till the conditions retuens true
  for (x = 50, y = 10;  x > 30  || y  < 5; x = x - 5,y++)
  {
    printf("%d, %d\n", x , y);
  }

  return 0;
}
sysads@linuxhint $ gcc 2.c -o 2
sysads@linuxhint $ ./2
50, 10
45, 11
40, 12
35, 13

Example-3: Nested for loop

When a for loop uses under another for loop then it is called nested for loop. The first loop will iterate for 5 times and the second loop will iterate for 8 times. When the value of the variable i and j are equal then the value of both variables will print.

#include <stdio.h>
int main()
{
  // Iterate the loop for 5 times
  for (int i=1; i<6; i++)
  {
    // Iterate the loop for 8 times
    for (int j=1; j<9; j++)
    {
      // Print the value of i and j when both are equal
      if (i == j)
         printf("%d, %d\n",i ,j);
    }
  }
  return 0;
}
sysads@linuxhint $ gcc 3.c -o 3
sysads@linuxhint $ ./3
1, 1
2, 2
3, 3
4, 4
5, 5

while loop:

The initialization variable is defined before while loop and the termination condition checks before entering the loop. For this reason, while loop is called an entry-controlled loop. The loop terminates when the condition returns false.

syntax:

while (condition)
{
  statements
}

Example 4: while loop to print a specific message

The following example shows the use of while loop. The variable, n is used as the counter of the loop and the loop will iterate 3 times. A specific message will print for each value of n. It will print “Good Morning” for 1, “Good Afternoon” for 2 and “Good Evening” for 3.

#include <stdio.h>
int main()
{
  // Initialization the variable
  int n = 1;
 
  // Check the condition
  while (n <= 3)
  {
    // Print the message based on the value of n
    if( n == 1)
      printf( "Good Morning\n");
    else if( n == 2)
      printf( "Good Afternoon\n");
    else
      printf( "Good Evening\n");

    //Increment the variable
    n++;
  }

  return 0;
}
sysads@linuxhint $ gcc 4.c -o 4
sysads@linuxhint $ ./4
Good Morning
Good Afternoon
Good Evening

Do-while loop:

This loop works like while loop but the termination condition is checked after entering the loop. For this reason, it is called an exit-controlled loop. Since this loop checks condition later, it will execute the statements at least one even the condition returns true or false.

Syntax:

do {
  statements
} while(condition)

Example-5: do-while loop to find the square of the number

The following code will take a number from the user and find out the square value of the number until the taken input is greater than 10. Do-while loop always checks the condition at the end of the loop. So, the square value of one number must be printed and the condition will be checked later.

#include <stdio.h>
int main()
{
  int n,result;

  do {
 
     // Take a number as input
     printf("Enter a number : ");
     scanf("%d", &n) ;

    // Find out the square of the number
    result = n * n;

    // Print the result
    printf("The square of %d is %d\n",n, result);

  }while (n < 10);

  return 0;
}
sysads@linuxhint $ gcc 5.c -o 5
sysads@linuxhint $ ./5
Enter a number : 20
The square of 20 is 400

C Loop Exercises

  • Write a C Program to calculate the sum of all even numbers from 10 to 50.
  • Write a C Program take a number of 3 digits and print the sum of the digits. [ if the input is 123 then the output will 6].
  • Write a C program to print the array elements in reverse form.
  • Write a C program to display the square root of 10 positive numbers taken from the user.
  • Write a C Program to calculate the sum of 50 to 100.

Conclusion:

This article shows some uses of loops in C program. But there are many other uses of loops in C, such as infinite loop, loop with a break statement, loop with continue statement, etc. The same problem can be solved by using any of the three loops mentioned loop. The coder selects the loop based on the problem to make the code efficient.

About the author

Eftakhar

I am a freelancer and blog writer. I love to write article on any computer basic and programming related topics.