While loop in C

While Loop in C Program

In this article, I am going to discuss the While loop in C Program with Examples. Please read our previous articles, where we discussed Switch Statements in C. The Looping Statements is also called as Iteration Statements in C. So, we can use the word Looping and Iteration and meaning are the same. Before understanding while loop in C, let us first understand what is a loop, why we need the loop and the different types of loops available in the C program. 

What is looping?

The process of repeatedly executing a statement or group of statements until the condition is satisfied is called ad looping. In this case, when the condition becomes false the execution of the loops terminates. The way of the repetition will form a circle that’s why iteration statements are called loops.

Why do we need looping?

The basic purpose of the loop is code repetition. In implementation whenever the repetitions are required than in place of writing the statements, again and again, we need to go for looping.

Iteration or looping Statements:

Iteration statements create loops in the program. It repeats the same code fragment several times until a specified condition is satisfied is called iteration. Iteration statements execute the same set of instructions until a termination condition is met. C provides the following loop for iteration statements:

  1. while loop
  2. for loop
  3. do-while loop
While loop in C:

A while loop is used for executing a statement repeatedly until a given condition returns false. Here, statements may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.

While loop Control flow in C

Syntax of While Loop in C:

Syntax of While Loop in C

While we are working with a while loop first we need to check the condition, if the condition is true then control will pass within the body if it is false the control should be pass outside the body.

When we are working with an iteration statement after execution of the body control will be pass back to the condition and until the condition become false it repeats ‘n’ number of times. If the condition is not false then we will get an infinite loop.

Program to understand while loop in C:
#include<stdio.h>
int main()
{
 int a=1;
 while(a<=4)
 {
  printf("%d", a);
  a++;
 }
 return 0;
}

Output: 1234

The variable a is initialized with value 1 and then it has been tested for the condition. If the condition returns true then the statements inside the body of while loop is executed else control come out of the loop. The value of a is incremented using ++ operator then it has been tested again for the loop condition.

Example: Print the no 1 to 5 using a while loop.
#include <stdio.h>
int main()
{
    int i = 1;
    while(i <= 5)
    {
        printf("%d ", i);
        i = i + 1;
    }
    return 0;
}

Output: 1 2 3 4 5

Example: Print the nos 10, 9, 8…. 1 using while loop
#include <stdio.h>
int main()
{
    int i; 
    i = 10;
    while(i >= 1)
    {
        printf("%d ", i);
        i = i - 1;
    }
    return 0;
}

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

Example: Print the numbers in the following format up to a given number and that number is entered from the keyboard.

2 4 6 8 …………………….. up to that given number

#include <stdio.h>
int main()
{
    int i, n; 
    printf("Enter a Number : ");
    scanf("%d", &n);
    i = 2;
    while(i <= n)
    {
        printf("%d ", i);
        i = i + 2;
    }
    return 0;
}

Output:

blank

Example: Enter a number and print the Fibonacci series up to that number using a while loop.
#include <stdio.h>
int main ()
{
  int i, n, j, k;
  printf ("Enter a Number : ");
  scanf ("%d", &n);

  i = 0;
  j = 1;
  printf ("%d %d ", i, j);
  k = i + j;
  while (k <= n)
    {
      printf (" %d", k);
      i = j;
      j = k;
      k = i + j;
    }

  return 0;
}

Output:

blank

Example: Enter a number from keyboard and then calculate the no of digit and sum of digit of that number using a while loop.

#include <stdio.h>
int main()
{
    int no, nd, sd, rem;
    printf("Enter a Number : ");
    scanf("%d", &no);
    nd = 0;
    sd = 0;
    while (no > 0)
    {
        rem = no % 10;
        nd = nd + 1;
        sd = sd + rem;
        no = no / 10;
    }   
    printf("The number of digit is %d", nd);
    printf("\nThe sum of digit is %d", sd);
    return 0;
}

Output:

blank

What are nested loops in C?

It is a concept of placing a loop within another loop i.e. A loop inside another loop is called nesting of loops. That is why nested loops are also called “loop inside the loop”. There can be any number of loops inside one another with any of the three combinations depending on the complexity of the given problem.

In implementation when we need to repeat the loop body itself n number of times then we need to go for nested loops. Nested loops can be designed up to 255 blocks.

Nested While Loop Syntax:

In nested while loop one or more statements are included in the body of the loop. In nested while loop, the number of iterations will be equal to the number of iterations in the outer loop multiplies by the number of iterations in the inner loop which is almost the same as nested for loop. Nested while loops are mostly used for making various pattern programs in C like number patterns or shape patterns.

What is nested loops in C?

EXPLANATION:
  1. Whenever we are working with a nested loop always execution process will start from outer loop condition i.e. condition1.
  2. If the outer loop condition will true then control will be passed within the outer loop body.
  3. In order to execute an outer loop body if any while statement occurs then those are called an inner loop.
  4. Whenever the inner loop occurs we need to check inner loop condition i.e. condition2.
  5. When the inner loop condition is TRUE then the control should be passed within the body of the inner loop, until the inner loop condition becomes FALSE inner loop only will be executed. When the inner loop condition becomes FALSE then control will be passed to the outer loop.
  6. Until the outer loop condition becomes FALSE complete body will be executed n number of times.
Example: WAP to print the following format.

blank

Program:

#include <stdio.h>
int main ()
{
    int i, n, in;
    printf ("ENTER  A NUMBER ");
    scanf ("%d", &n);
    i = 1;
    while (i <= n)
    {
      printf ("\n");
      in = 1;
      while (in <= i)
   {
     printf ("%d ", in);
     in = in + 1;
   }
      i = i + 1;
    }
    return 0;
}
Example: WAP to print the following format:

blank

Program:

#include <stdio.h>
int main()
{
    	int i, n, dn;
    	printf("ENTER  A NUMBER ");
    	scanf("%d", &n);
    	i = n;
    	while(i >= 1)
    	{
        		printf("\n");
        		dn = i;
        		while(dn >= 1)
        		{
                		printf("%d ", dn);
                		dn = dn - 1;
        		}
        		i = i - 1;
    	}    
    	return 0;
}
Example: WAP to print the following format:

Nested While Loop in C

Program:

#include <stdio.h>
int main ()
{
    int a = 1, b = 1;
    while (a <= 5)
    {
       b = 1;
       while (b <= 5)
    {
       printf ("%d ", b);
       b++;
    }
       printf ("\n");
       a++;
    }
    return 0;
}
What is the pre-checking process or entry controlled loop?

The pre-checking process means before the evaluation of the statement block conditional part will be executed.

When we are working with a while loop always pre-checking process will occur. The loop in which before executing the body of the loop if the condition is tested first then it is called an entry controlled loop.

While loop is an example of entry controlled loop because in while loop before executing the body first condition is evaluated, if the condition is true then the body will be executed otherwise the body will be skipped.

In the next article, I am going to discuss Do While Loop in C Program with examples. Here, in this article, I try to explain While Loop in C Program with examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *