In this article while loop in C we give the information about while is an entry-controlled loop statement. The test-condition is evaluated and if the condition is true, the main part of the loop is executed.
Looping Statements in C
Introduction
In C programming, a loop is a control structure that allows a block of code to be executed repeatedly based on a given condition.
Loops help to reduce code redundancy and make programs more efficient by performing repetitive tasks automatically.
Steps Involved in Looping Process
The looping process generally involves the following four steps:
- Setting and initialization of condition variables
→ The loop control variable is initialized before the loop starts. - Execution of statements in the loop
→ The loop body executes one or more times as long as the condition is true. - Test for the specified value of the condition variable
→ Before (or after) each iteration, the condition is tested to decide whether to continue or stop. - Increasing or updating the condition variable
→ The loop control variable is incremented or decremented so that the condition eventually becomes false, ending the loop.
Types of Loops in C
C language provides three looping structures:
- while loop
- do-while loop
- for loop
While Loop in C
Definition
The while loop is an entry-controlled loop, meaning the test condition is evaluated before executing the loop body.
- If the condition is true, the statements inside the loop are executed.
- If the condition is false, the loop terminates and control passes to the next statement.
Syntax
while (condition)
{
// Body of loop // Statements to be executed
}

Explanation
- The condition is checked first.
- If it is true, the body of the loop executes.
- After executing the body, the control returns to the condition.
- Steps 1–3 repeat until the condition becomes false.
- When false, the program continues with the next statement after the loop.
Flow of Execution
- The condition is evaluated.
- If true → loop body executes.
- Control returns to the condition.
- The loop continues until the condition becomes false.
- Once false → control moves out of the loop.
Example 1: Print Numbers from 1 to 5
#include <stdio.h>
#include <conio.h>
void main()
{
int i = 1;
while (i <= 5)
{
printf(“%d\n”, i);
i++;
}
getch();
}
Output:
1
2
3
4
5
Example 2: Sum of First 5 Natural Numbers
#include <stdio.h>
#include <conio.h>
void main()
{
int i = 1, sum = 0;
while (i <= 5)
{
sum = sum + i;
i++;
}
printf(“Sum = %d”, sum);
getch();
}
Output:
Sum = 15
Key Points
- The condition must eventually become false, otherwise the loop runs infinitely.
- The initialization must be done before entering the while loop.
- The increment/decrement must be inside the loop to update the condition properly.
Example of Infinite While Loop
#include <stdio.h>
int main()
{
while(1)
{
printf(“This is an infinite loop.\n”);
}
return 0;
}
Explanation:
This loop will execute endlessly because the condition (1) is always true.
Example: Display Sum of First 10 Natural Numbers
#include <stdio.h>
#include <conio.h>
void main()
{
int p = 1, n_sum = 0;
while (p <= 10)
{
n_sum = n_sum + p;
p++;
}
printf(“\n Display 1st ten natural number sum = %d”, n_sum);
getch();
}
Output:
Display 1st ten natural number sum = 55
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining