While Loop in C

The while loop in C Programming is to repeat a block of statements for a given number of times until the given condition is False. While loop in C starts with the condition, if the condition is True, then statements inside the while loop will be executed. If the given condition is false, then it won’t be performed at least once. It means while loop may run zero or more time and the syntax of while loop in C programming is:

While Loop C Programming Syntax

The syntax of While loop in C Programming is as follows:

While ( Condition )
 {
   statement 1;
   statement 2;
    ………….
 }
This is the statement Outside the While Loop but inside the main() Function

First, the compiler will check for the condition inside the While loop. If the condition is True, the statement or group of statements under the while loop block will execute. If the condition is False, the compiler will come out of the loop and execute other statements outside the while loop.

For a single statement inside a while loop in C Programming, curly braces not needed. However, if we omit them for multiple statements, the compiler will execute the first statement only. It is always good practice to use braces all the time.

Flow Chart for While loop in C Programming

The following screenshot shows you the flow chart of this While loop in C programming language.

 While Loop In C Flow Chart

At the beginning of the loop, While loop in C checks for the condition.

  1. If the condition is True, then it will execute the statements inside the loop.
  2. Next, we have to use Increment & Decrement Operator inside the while loop to increment and decrements the value. Please refer Increment and Decrement Operator in C article to understand the functionality
  3. Again it will check for the condition after the value incremented. As long as the condition is True, the statements inside the while loop will execute.
  4. If the condition is False, the compiler will exit from the While loop

Let us see the While loop example for better understanding

While Loop in C Programming Example

This while loop example program allows the user to enter an integer value below 10. By using this value, the compiler will add those values up to 10.

/* While Loop in C Programming example */

#include <stdio.h >

int main()
{
  int number, total=0;

  printf("\n Please Enter any integer below 10 \n ");
  scanf("%d", &number);

  while (number <= 10)
   {
     total = total+number;
     number++;
   }

  printf("\n Value of Total From the While  Loop is: %d \n", total); 
  return 0;
}

ANALYSIS

  1. In this C while loop example, User will enter any value below 10 and total variable is initialized to 0.
  2. Next, the user-entered value will assign to the number variable. Then, the given number will test against the condition inside a while loop.
  3. If the condition results true, the number added to the total. Otherwise, it will exit from the loop
  4. In the next line, we used ++ operator to increment the number value. After incrementing, the process will repeat until the condition results False.

We are going to enter number = 6. It means, total = 6+7+8+9+10 = 40

While Loop in C Programming 1

Infinite While Loop in C Programming

If you forgot to increment or decrement the value inside the while loop, then while loop in C will execute infinite times, also called an infinite loop. For example:

/* Infinite While Loop in C Programming example */
 
#include<stdio.h> 

int main()
{
  int x = 1;

  while (x < 10)
   {
     printf("Value From the While  Loop is: %d\n", x);    

   }
 return 0;
}
Value From the While  Loop is: 1
Value From the While  Loop is: 1
Value From the While  Loop is: 1
Value From the While  Loop is: 1
Value From the While  Loop is: 1
Value From the While  Loop is: 1
...

Here x is always 1, and x is always less than 10. So, while loop statement will execute infinite times (infinity loop). Now, let us add an increment operator (x++) inside the C while loop to the above example.

/* While Loop in C Programming example */

#include<stdio.h> 

int main()
{
  int x = 1;

  while (x < 10)
   {
     printf(" Value From the While  Loop is: %d \n ", x); 
     x++;   
   }
  return 0;
}

Now, when it reaches 10 the while condition will fail. Let us see the C Programming output

 Value From the While  Loop is: 1
 Value From the While  Loop is: 2
 Value From the While  Loop is: 3
 Value From the While  Loop is: 4
 Value From the While  Loop is: 5
 Value From the While  Loop is: 6
 Value From the While  Loop is: 7
 Value From the While  Loop is: 8
 Value From the While  Loop is: 9