← Back to C Course | Chapter 4: Control Flow | Lesson 7 of 11

C while Loop

A while loop repeats steps as long as a condition stays true, like stirring soup until it is hot. It checks before each round.
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to update the loop variable, creating an infinite loop.
  2. Putting a semicolon after while (x < 5);, making the loop body empty.
  3. Getting an off-by-one error by using <= instead of <.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.