WHILE LOOP IN C

Dev Frank
3 min readJan 16, 2024

--

While loop is another loop statement in c. The working of while loop and for loop are almost the same. The only difference is the syntax.

Whatever program you can write with while loop you can as well write it with for loop and do while loop.

It is an entry control loop i.e. the condition will be checked first at the entry time only, if the condition is true then only will the control go within the body of the loop. If the condition is false, it won’t go within the body of the loop, it will exit from the loop.

SYNTAX OF WHILE LOOP

while(condition)
{
//body of the loop
}
  1. The while keyword is followed by a pair of parentheses ( ) containing a condition. The condition is a Boolean expression that determines whether the loop should continue or not.
  2. The code block within the curly braces { } immediately following the while keyword contains the statements that will be executed repeatedly as long as the condition remains true.
  3. The condition is evaluated before each iteration of the loop. If the condition is true, the code block is executed; if the condition is false, the loop is exited, and the program continues with the next statement after the loop.

In while loop we also have those three things loops requires

  • Initialization
  • Terminating Condition
  • Update (Increment / Decrement)

The initialization is done before the while loop, while the terminating condition and update(increment/decrement) will be done inside the loop.

Initialization
while(Terminating condition)
{
//statement 1;
// -----
// -----
// statement n;

update/modify (increment/ decrement);
}

As you can see the difference between for loop and while loop is that in for loop initialization, terminating condition, Increment & Decrement are all done in the same place (inside the parenthesis) whereas while loop is done as explained above.

EXPLANATION OF WHILE LOOP WITH THE AID OF A FLOWCHART

NB: It works the same way as the for loop.

Example

#include <stdio.h>
#include <conio.h>

int main(void)
{
int i = 1;

while(i <= 10)
{
printf("%d\n", i);
i++;
}
getch();

return(0);

This program prints numbers from i to 10 using while loop. The variable i was initialized to 1, then the while loop was passed a condition (if i was less than or equal to 10).

The condition was true and the control enters the while loop and evaluated the printf statement i.e. it printed 1 and goes to a newline. After the printf statement had been evaluated, it moves to the increment (i++) and increments the value of i which is 1.
Then the updated value of i becomes. The control checks the condition to see if 2 is less than than or equal to 10, which is true, then it prints the value of i (2) and goes to a newline. It does this till i becomes 10.

When i becomes 10 the value is still true, then it prints 10 and goes to a new line. The value of i is then incremented to 11 but this time making it false, because 11 is not less than or equal to 10. Then it automatically exits loop.

TRY THIS !!!

#include <stdio.h>

int main() {
int marbles = 5;


while (marbles > 0) {
printf("Rolling a marble!\n");

marbles--;

if (marbles > 0) {
printf("I have %d marbles left.\n", marbles);
} else {
printf("Oops, no more marbles left!\n");
}
}

printf("I'm done playing with marbles. That was fun!\n");

return 0;
}

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.