While Loop Syntax in C
What is Loop?
The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make up a loop in C. The control statement is a collection of conditions that tells the loop's body to keep running until a certain condition is met.
While Loop:
The while loop is an entry-controlled loop. Entry controlled loop means before starting the loop's body, the condition is verified. In other words, the condition won't ever execute if it is false.
Most repetitive operations are carried out using the while loop. It doesn't have initialization or update statements as a for loop does.
It requires three stages to accomplish.
- Initialization of a variable
e.g. int a=1;
- condition
e.g. while(a<=6);
- Variable increment or decrement
e.g. a++ or ++a , a-- or a=a-1;
while loop Syntax:
Initialization of a variable ;
while(condition)
{
//executed code
Statements;
Increment or decrement of a variable;
}
While loop Syntax Description:
- Variable initialization:
Before starting the while loop, first of all, we initialize a variable with some value.
- Condition:
Before the while loop's code block, the condition expression is assessed. To determine whether to continue the while loop or end it, a boolean expression is used. If the condition expression evaluates to true, the code block inside the while loop run; otherwise, the while loop gets terminated, and control is transferred to the following statement.
- Statements:
Statements can be included in a while loop in any number, even zero. In C programming, curly braces are not required for a single statement contained within a while loop. The compiler will only execute the first statement if they are absent from several statements. Braces should always be worn continuously.
- Variable Increment or Decrement:
The while loop will never come to an end if we don't increase or decrease the value of variable inside of it since otherwise, the condition will always be true. The loop's code will run endlessly as a result of this.
Flow Chart for while loop:

The While loop initially looks for the condition.
- The statements inside of it will be executed if the condition is True.
- The value must then be incremented and decremented inside it using the Increment & Decrement Operator. For information on the functionality, please see the C article on the Increment and Decrement Operator.
- After the value is increased, it will check once more time for the condition. The statements inside the condition shall be carried out so long as it is True.
Using the While Loop in C Programming:
#include <stdio.h >
int main()
{
int x,i, sum=0;
printf(" Please Enter any integer:\n ");
scanf("%d", &i);
x=1;
while (x <= i)
{
sum = sum+x;
x++;
}
printf("\n Value of sum is: %d ", sum);
return 0;
}
Explanation:
In above code, we have created three int data type variables, “x”, “i”, and “sum”.
The user can enter an integer value lower than “i” with this software. This value will cause the compiler to add those values up to “i”.
The sum variable is initialized to zero in this while loop example, and the user will input any amount lower than “i”.
Next, the “x” variable will receive the user-entered value. The supplied “x” will then be checked against the requirement.
The amount is added to the sum if the condition is true. In every other case, the iteration will end.
The ++ operator increases the “x” value in the following line. When the expression returns False, the operation will keep repeating after incrementing.
Program Output :

Another program for while loop:
#include<stdio.h>
int main()
{
int x=1;
while(x<=10)
{
printf("%d\n",x);
x++;
}
return 0;
}
Code Explanation:
In the above code, we used a while loop to display a succession of numbers from 1 to 10.
- We have set the value of the variable “x” to 1. The variable is initialized with the value one because we are printing from 1 to 10. Assign the number 0 during initialization if you wish to start printing at 0.
- Given the condition (x=10) in a while loop, the body of the loop will be executed up to the point where “x” equals 10. Following that, the loop will be broken, and control will shift outside of it.
- A print function and an increment operation are provided in the body of a loop so that the value printed per loop execution can be increased. Initially having the value 1, “x” will change to 2 after execution and then to 3 on the following run. The series will be printed on the console, and the loop will be broken whenever the value reaches 10.
Program Output :
