c
C Programming Tutorial : Introduction

C Loops: While, Do While, with Example

Loops are used when we want to execute a part of the program or a block of statements several times. For example, suppose we want to print “C is the best” 10 times. One way to get the desired output is, to write 10 printf statements, which is not preferable.

Other way is using loop, we can write one loop statement and only one printf statement, and this approach is better than the first one. With the help of loop, we can execute a part of the program repeatedly till some condition is true.

In this tutorial, you will learn-

  1. While Loop
  2. Do-While loop
  3. For loop
  4. Break Statement
  5. Continue Statement

Types of Loops

Depending upon the position of a control statement in a program, a loop is classified into two types:

  1. Entry controlled loop
  2. Exit controlled loop

In an entry-controlled loop, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop.

In an exit-controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking loop.

The control conditions must be well defined and specified otherwise the loop will execute an infinite number of times. The loop that does not stop executing and processes the statements number of times is called as an infinite loop. An infinite loop is also called as an “Endless loop.” Following are some characteristics of an infinite loop:

  1. No termination condition is specified.
  2. The specified conditions never meet.

‘C’ programming language provides us with three types of loop constructs:

  1. The while loop
  2. The do-while loop
  3. The for loop

while loop

A while loop is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop. If a condition is true, then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.

Syntax -1

while(condition) 
             statements;
             ...........
             ...........


 

Syntax -2

while(condition) 
{
             statements;
             statements;
        	............
        	............
}
 
while-loops-featured--flow-chart

Like if-else statement here also we can have either a single statement or a block of statements, and here it is known as the body of loop. Now let us see how this -loop works.

Example-1

/* Program to print the numbers from 1 to 10 using while. Loop*/

#include<stdio.h>
#include<conio.h>

void main ( )
{
    int i=1;
    while(i<=10)
        {
            printf("%d\t",i) ;
            i=i+1;  /*Statement that changes the value of condition*/
        }
    printf('\n");
    getch();
}

Output:
1 2 3 4 5 6 7 8 9 10
 

The above program illustrates the use of while loop. In the above program, we have printed series of numbers from 1 to 10 using a while loop. Here initially the condition ( i<=10) is true. After each iteration of the loop, value of i increases, when the value of is equals to 11 the condition becomes false and the loop terminates

  1. We have initialized a variable called ‘i’ with value 1. We are going to print from 1 to 10 hence the variable is initialized with value 1. If you want to print from 0, then assign the value 0 during initialization.
  2. In a while loop, we have provided a condition (i<=10), which means the loop will execute the body until the value of ‘i’ becomes 10. After that, the loop will be terminated, and control will fall outside the loop.
  3. In the body of a loop, we have a print function to print our number and an increment operation to increment the value per execution of a loop. An initial value of ‘i’ is 1, after the execution, it will become 2, and during the next execution, it will become 3. This process will continue until the value becomes 10 and then it will print the series on console and terminate the loop.

Example-2

/* Program to print the sum digits of any entered number using while. Loop*/

#include <stdio.h>
#include <conio.h>

void main()
{
   int n, sum = 0, rem;
   printf("Enter the Number: ");
   scanf("%d", &n);
   while (n>0)
   {
      rem = n%10;
      sum = sum + rem;
      n= n / 10;
   }
   printf("Sum of digits of %d = %d\n", n, sum);
   getch();
}
output:
Enter the number: 1452
Sum of digits = 12
 

Here we are extracting the digits of the number from right to left and then these digits are added one

by one to the variable sum. Note that the variable sum is initialized to 0. This is because we are adding

some numbers to it, and if not initialized then these numbers will be added to garbage value present. In it Let’s see how the loop works when the value of n is 1452.

Before loop starts            rem = garbage value,      sum=0,                                n=1452

After 1st iteration            rem=1452%10=2,            sum=0+2=2,                      n=145

After 2nd iteration          rem=145%10=5,              sum=2+5=7,                      n=14

After 3rd iteration           rem=14%10=4,                 sum=7+4=11,                    n=1

After 4th iteration            rem=1%10=1,                  sum=U+1=12,                   n=0

Now since the value of n is equal to zero, hence the condition (n>0) becomes false and the loop stops.

/*Program to find the product of digits of any number*/
#include<stdio.h>
#include<conio.h>

void main ()
{
    Int n,prod=1,rem;
    printf ("Enter the number ");
    scanf("%d",&n) ;
    while(n>0)
        {
            rem=n%10;
            prod=prod*rem;
            n=n/10;
        }
    printf("Product of digits = %d\n",prod);
    getch();
}

Output:
Enter the number : 234
Product of digits = 24