C Programs Tutorials | IT Developer
IT Developer

C Programming - C Loops



Share with a Friend

C Programming - C Loops

Loops in C Programming

In C programming, loops are used to execute a block of code repeatedly based on a given condition. There are three main types of loops in C:

  1. for loop
  2. while loop
  3. do-while loop

Each type of loop is used depending on the condition and when you want to check the condition (before or after executing the loop body).

  1. for Loop

The for loop is typically used when you know in advance how many times the loop needs to run. It has three parts:

  • Initialization: The counter variable is initialized.
  • Condition: The loop runs as long as this condition is true.
  • Update: The counter is updated after each iteration.

Syntax:

C

for (initialization; condition; update) {

    // Code to be executed

}

Example:

C

#include <stdio.h>

int main() {

    for (int i = 0; i < 5; i++) {

        printf("i = %d\n", i);

    }

    return 0;

}

Explanation:

  • The loop starts with i = 0.
  • The condition i < 5 is checked. If true, the code inside the loop executes.
  • After each iteration, i is incremented (i++).
  • The loop runs until i reaches 5.

Output:

i = 0

i = 1

i = 2

i = 3

i = 4

  1. while Loop

The while loop is used when the number of iterations is not known in advance and depends on a condition. The condition is evaluated before executing the loop body.

Syntax:

C

while (condition) {

    // Code to be executed

}

Example:

C

#include <stdio.h>

int main() {

    int i = 0;

    while (i < 5) {

        printf("i = %d\n", i);

        i++;

    }

    return 0;

}

Explanation:

  • The condition i < 5 is checked before each iteration.
  • As long as the condition is true, the code inside the loop runs.
  • i is incremented each time until it reaches 5, at which point the loop terminates.

Output:

i = 0

i = 1

i = 2

i = 3

i = 4

  1. do-while Loop

The do-while loop is similar to the while loop, but with one key difference: the condition is checked after executing the loop body. This ensures that the loop body is always executed at least once, even if the condition is false initially.

Syntax:

C

do {

    // Code to be executed

} while (condition);

Example:

C

#include <stdio.h>

int main() {

    int i = 0;

    do {

        printf("i = %d\n", i);

        i++;

    } while (i < 5);

    return 0;

}

Explanation:

  • The code inside the loop is executed first before checking the condition.
  • The condition i < 5 is checked after the loop body.
  • The loop runs until the condition becomes false.

Output:

i = 0

i = 1

i = 2

i = 3

i = 4

Comparison of Loops

Feature for Loop while Loop do-while Loop

Condition Check

Before loop execution

Before loop execution

After loop execution

Use Case

Known number of iterations

Unknown number of iterations (pre-condition)

Unknown number of iterations (post-condition)

Guarantee of Execution

May not execute at all if the condition is false initially

May not execute if the condition is false initially

Will always execute at least once

Example Use Case

Iterating over a range of numbers

Iterating until a condition becomes true

Repeating a menu or prompt until the user provides valid input

break and continue Statements in Loops

In C, break and continue are used to control the flow of execution inside loops:

  • break: Exits the loop immediately, regardless of the condition.
  • continue: Skips the current iteration and moves to the next iteration.

Example with break and continue:

C

#include <stdio.h>

int main() {

    for (int i = 0; i < 10; i++) {

        if (i == 5) {

            break;  // Exit the loop when i equals 5

        }

        if (i % 2 == 0) {

            continue;  // Skip the iteration if i is even

        }

        printf("i = %d\n", i);  // Print only odd numbers less than 5

    }

    return 0;

}

Explanation:

  • The loop will break when i equals 5.
  • Even numbers are skipped due to the continue statement, so only odd numbers before 5 are printed.

Output:

i = 1

i = 3

Summary of C Loops:

  • for loop: Best when you know the number of iterations beforehand.
  • while loop: Best when the loop condition should be evaluated before each iteration and the number of iterations is unknown.
  • do-while loop: Best when the loop body should be executed at least once before checking the condition.
  • break: Exits the loop immediately.
  • continue: Skips the current iteration and moves to the next one.

Loops are essential in C for repeating tasks and iterating over data, and they form the foundation of many algorithms and control structures.