Menu
Coddy logo textTech

While Loop

Part of the Fundamentals section of Coddy's C journey — lesson 38 of 63.

The while loop lets you repeatedly execute a block of code as long as a specified condition is true.

Here's the basic syntax:

while (condition) {
    // code to be repeated
}

Let's create a simple counter:
Initialize a counter variable

int count = 1;

Create a while loop that runs as long as count is less than or equal to 5

while (count <= 5) {
    printf("%d ", count);
    count++;
}

After executing the code, the output will be:

1 2 3 4 5

The loop continues until count becomes 6, at which point the condition count <= 5 becomes false, and the loop ends.

challenge icon

Challenge

Easy

Write a program that reads an integer n from the user and prints all even numbers from 2 to n (inclusive) using a while loop.

For example, if the user enters 10, your program should print:

2 4 6 8 10

Cheat sheet

The while loop repeatedly executes code as long as a condition is true:

while (condition) {
    // code to be repeated
}

Example - counting from 1 to 5:

int count = 1;
while (count <= 5) {
    printf("%d ", count);
    count++;
}

Output: 1 2 3 4 5

Try it yourself

#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    
    // Your code here
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals