Menu
Coddy logo textTech

Infinite Loops

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

An infinite loop is a loop that continues indefinitely because its termination condition is never met. While usually undesirable, there are some situations where infinite loops can be useful, such as in embedded systems or for continuously running programs.

In C, you can create an infinite loop using various constructs:

  1. Using a while loop:

    while (1) {
        // code to be executed
    }
  1. Using a for loop:

    for (;;) {
        // code to be executed
    }
  1. Using a do-while loop:

    do {
        // code to be executed
    } while (1);

To exit an infinite loop, you typically use a break statement or modify a control variable within the loop based on some condition.

Be cautious when using infinite loops, as they can cause your program to hang if not properly managed.

Cheat sheet

An infinite loop continues indefinitely because its termination condition is never met.

Create infinite loops using:

While loop:

while (1) {
    // code to be executed
}

For loop:

for (;;) {
    // code to be executed
}

Do-while loop:

do {
    // code to be executed
} while (1);

Exit infinite loops using break statement or by modifying a control variable based on some condition.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

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

All lessons in Fundamentals