---
title: 2.11 while Loop
description: "The `while` loop repeatedly executes a block of code as long as its condition evaluates to a non-zero value. ``` #include int main() { int i = 1; while (i"
url: "https://magenta-tarsier-454184.hostingersite.com/docs/c-programming/control-flow/2-11-while-loop/"
updated: "2026-09-06"
category: C Programming / Control Flow
---

# 2.11 while Loop

The `while` loop repeatedly executes a block of code as long as its condition evaluates to a non-zero value.

```
#include <stdio.h>

int main()
{
    int i = 1;

    while (i <= 5)
    {
        printf("%dn", i);
        i++;
    }

    return 0;
}
```

### Example Output

```
1
2
3
4
5
```

### Explanation

The condition is evaluated before each iteration:

```
while (i <= 5)
```

As long as the condition is non-zero, the loop body executes.

The value of `i` is incremented after each iteration:

```
i++;
```

When `i` becomes `6`, the condition `i <= 5` evaluates to `0`, so the loop terminates.

Because the condition is checked before the loop body, a `while` loop can execute **zero times** if its initial condition is false.
