How to create a while loop in C
As long as the given condition stands true, the While Loop can be used to execute a set of statements repetitively. The While loop depends on the following:
- Condition: This controls the termination of while loop. If the condition becomes false, the while loop will terminate.
- Execution statements: This completes the tasks assigned to the while loop.
Flowchart
How it works
The While loop will first check the value of the condition, and if it is true then the statement (or multiple statements) will be executed once.
Afterward, the condition will be evaluated again and if it stands true, the statements will be executed again, and so on. The While loop will only stop when the condition is found to be false.
Example
The following code adds “3” to the answer until the answer becomes 30.
#include <stdio.h>int main () {int answer=0;//while loopwhile( answer != 30 ) {answer=answer+3; //statements to be executed}printf("value of answer = %d\n", answer);return 0;}
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved