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 it will be executed. If the given condition is false, then it won’t be performed at least once. It means while loop in c programming may run zero or more time and the syntax of it 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 block will execute. If the condition is False, the compiler will come out of it 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, While loop in C checks for the condition.

  1. If the condition is True, then it will execute the statements inside of it.
  2. Next, we have to use Increment & Decrement Operator inside it 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 it will execute.
  4. If the expression evaluated to False, the compiler will exit from the While loop

Let us see the example for better understanding

While Loop in C Programming Example

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

  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.
  3. If the condition results true, the number added to the total. Otherwise, it will exit from the iteration.
  4. In the next line, we used ++ operator to increment the number value. After incrementing, the process will repeat until the expression results False.
#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;
}

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 it will execute infinite times, also called an infinite loop. For example:

#include<stdio.h> 

int main()
{
  int x = 1;

  while(x < 10)
   {
     printf("Value = %d\n", x);    

   }
 return 0;
}
Value = 1
Value = 1
Value = 1
Value = 1
Value = 1
...

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

#include<stdio.h> 

int main()
{
  int x = 1;

  while(x < 10)
   {
     printf("Number = %d \n ", x); 
     x++;   
   }
  return 0;
}

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

Number = 1
Number = 2
Number = 3
Number = 4
Number = 5
Number = 6
Number = 7
Number = 8
Number = 9