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 programming starts with the condition, if the condition is True, then statements inside it will be executed. If the given condition is false, it won’t be performed at least once. It means while loop may run zero or more time, and the syntax of it is:

While Loop C Programming Syntax

The syntax of the C While loop is as follows:

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

First, the compiler will check for the condition inside the C 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 are 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

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

While Loop Flow Chart

At the beginning, the C While loop 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 decrement the value. Please refer to the Increment and Decrement Operator in the C article to understand the functionality.
  3. Again it will check for the condition after the value is incremented. The statements inside it will execute as long as the condition is True.
  4. If the expression is evaluated as False, the compiler will exit from the While loop.

Let us see the example for a 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, the User will enter any value below 10, and the 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 is added to the total. Otherwise, it will exit from the iteration.
  4. We used the ++ operator in the next line to increment the number value. After incrementing, the process will repeat until the expression results as 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 the 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 forget to increment or decrement the value inside the C while loop, it will execute infinite times, also called an infinite loop. For example:

// 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 while loop to the above example.

// Infinite While Loop in C Programming Solution
#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