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

C for Loop

A for loop repeats steps a set number of times, like doing ten jumping jacks while counting. The start, stop, and counting all sit in one line.
Syntax
c
for (initialization; condition; update) {
  // body
}

What is a for Loop?

A for loop combines a variable's initialization, its exit condition, and its update step into a single line, making the loop's entire lifecycle visible at a glance instead of scattered across separate statements.

Example: What is a for Loop?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Standard Counter

A for loop is the natural choice whenever you already know exactly how many times you want to repeat something, since the fixed number of iterations can be baked directly into the loop's condition.

Example: Standard Counter

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 1; i <= 5; i++) {
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing Even Numbers

Adjusting a for loop's initial value and its update step (incrementing by 2 instead of 1, for example) lets you print only even numbers within a range without needing an extra if check inside the loop body.

Example: Printing Even Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i <= 10; i += 2) {
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Summing Sequences

An accumulator variable declared outside the loop, updated by adding the loop counter's value on each pass, is the standard pattern for summing a sequence of numbers using a for loop.

Example: Summing Sequences

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare sum and set it to 0
	int sum = 0;
	// Loop: repeat while the condition holds
	for (int i = 1; i <= 5; i++) {
		sum += i;
	}
	// Print formatted text to the screen
	printf("%d", sum);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Decremental For Loop

To count downward with a for loop, start the counter at a high value, use a greater-than-or-equal condition, and decrement instead of increment on each pass -- the same three-part structure, just running in reverse.

Example: Decremental For Loop

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 5; i >= 1; i--) {
		// Print formatted text to the screen
		printf("%d ", 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. Using <= with an array length, like i <= n, reading one element past the end.
  2. Putting a semicolon after for (...);, which makes the body empty.
  3. Using commas instead of semicolons inside the parentheses.

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.