C while Loop
In this page:
while (condition) {
// body
// update the loop variable
}
What is a while Loop?
A while loop checks its condition before every iteration, including the very first one -- if the condition starts out false, the loop body never runs at all, unlike a do-while loop which always runs once regardless.
Example: What is a while Loop?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 10
int x = 10;
// Repeat while x < 5 is true
while (x < 5) {
// Print formatted text to the screen
printf("never runs");
}
// Print formatted text to the screen
printf("Loop skipped since condition was false");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Counting Up
Using a counter variable that increases each pass, a while loop can print an ascending sequence of numbers -- initializing the counter before the loop and incrementing it inside the body on each iteration.
Example: Counting Up
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 1
int i = 1;
// Repeat while i <= 5 is true
while (i <= 5) {
// Print formatted text to the screen
printf("%d ", i);
i++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Counting Down
By initializing a counter at a high value and decreasing it each iteration instead of increasing it, the same while structure counts downward -- useful for countdowns or processing a range in reverse.
Example: Counting Down
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 5
int i = 5;
// Repeat while i >= 1 is true
while (i >= 1) {
// Print formatted text to the screen
printf("%d ", i);
i--;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Sum of First N Numbers
Accumulating a running total inside a while loop -- adding the current counter value to a separate sum variable on each pass -- is the standard pattern for computing something like the sum of the first N integers.
Example: Sum of First N Numbers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int i = 1, sum = 0;
// Repeat while i <= 5 is true
while (i <= 5) {
sum += i;
i++;
}
// Print formatted text to the screen
printf("%d", sum);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Infinite Loop Prevention
If the loop's condition never becomes false -- typically because you forgot to update the variable it depends on -- the loop runs forever, freezing the program; always double check that every code path inside the loop moves the condition toward becoming false.
Example: Infinite Loop Prevention
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 0
int i = 0;
// Repeat while i < 5 is true
while (i < 5) {
// Print formatted text to the screen
printf("%d ", i);
i++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to update the loop variable, creating an infinite loop.
- Putting a semicolon after
while (x < 5);, making the loop body empty. - Getting an off-by-one error by using
<=instead of<.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: