Loops in C

Looping in C is a fundamental concept that allows you to execute a block of code repeatedly. There are three primary types of loops in C:

  1. for loop

  2. while loop

  3. do-while loop

Each type of loop can be used depending on the specific requirement of the situation.

1. for loop

The for loop is used when the number of iterations is known before entering the loop. The syntax is:

for (initialization; condition; increment) {
    // code to be executed
}

Sample Code:

#include <stdio.h>

int main() {
    int i;
    for (i = 0; i < 5; i++) {
        printf("%d ", i);
    }
    return 0;
}

This will output: 0 1 2 3 4

2. while loop

The while loop is used when the number of iterations is not known and depends on a condition. The loop will continue as long as the condition is true.

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

Sample Code:

#include <stdio.h>

int main() {
    int i = 0;
    while (i < 5) {
        printf("%d ", i);
        i++;
    }
    return 0;
}

This will output: 0 1 2 3 4

3. do-while loop

The do-while loop is similar to the while loop, but the code block will execute at least once because the condition is evaluated after the block is executed.

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

Sample Code:

#include <stdio.h>

int main() {
    int i = 0;
    do {
        printf("%d ", i);
        i++;
    } while (i < 5);
    return 0;
}

This will output: 0 1 2 3 4

Nested Loops

Loops can be nested within other loops to perform multi-dimensional iteration.

Sample Code:

#include <stdio.h>

int main() {
    int i, j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("(%d, %d) ", i, j);
        }
        printf("\n");
    }
    return 0;
}

This will output a 3x3 grid of coordinates:

(0, 0) (0, 1) (0, 2) 
(1, 0) (1, 1) (1, 2) 
(2, 0) (2, 1) (2, 2)

Infinite Loops

An infinite loop occurs when the terminating condition will never be false. These are often used deliberately to keep a program running until an external event occurs.

Sample Code:

#include <stdio.h>

int main() {
    while (1) {
        // This code will run forever
    }
    return 0;
}

Loop Control Statements

  • break; - Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

  • continue; - Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Sample Code with break and continue:

#include <stdio.h>

int main() {
    int i;
    for (i = 0; i < 10; i++) {
        if (i == 5) {
            break; // Exit the loop when i is 5
        }
        if (i % 2 == 0) {
            continue; // Skip the rest of the loop body for even numbers
        }
        printf("%d ", i);
    }
    return 0;
}

This will output: 1 3

Loops are a powerful feature in C that allow for efficient repetition of code blocks. They are used in almost every non-trivial program to perform tasks repeatedly.