Various loop control in C with example

Loop control in C: In C programming, loops are used to execute a block of code repeatedly. There are several types of loop control structures in C, each serving different needs and conditions:

for loop control in C

The for loop is used when the number of iterations is known beforehand. It is a compact way to iterate over a range of values.

Syntax:

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

Example:

for (int i = 0; i < 10; i++) {
printf(“%d\n”, i);
}

In this example, the loop initializes i to 0, checks if i is less than 10, and increments i by 1 after each iteration. The loop prints the values from 0 to 9.

while loop control in C

The while loop is used when the number of iterations is not known in advance. It continues to execute as long as the given condition is true.

Syntax:

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

Example:

int i = 0;
while (i < 10) {
    printf("%d\n", i);
    i++;
}

In this example, the loop continues to run as long as i is less than 10, printing the values from 0 to 9.

do-while Loop control in C

The do-while loop is similar to the while loop, but it guarantees that the code block will be executed at least once before the condition is tested.

Syntax:

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

Example:

int i = 0;
do {
    printf("%d\n", i);
    i++;
} while (i < 10);

In this example, the loop prints the values from 0 to 9, executing the code block first and then checking the condition.

break Statement in C

The break statement is used to exit a loop prematurely, regardless of the loop’s condition.

Example:

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    printf("%d\n", i);
}

In this example, the loop prints the values from 0 to 4 and then exits when i equals 5.

continue Statement in C

The continue statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration.

Example:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue;
    }
    printf("%d\n", i);
}

In this example, the loop prints the odd values from 1 to 9, skipping even values.

goto Statement in C

Although not commonly used, the goto statement allows you to jump to another part of the program. It can be used to exit loops or for creating loops, but it’s generally discouraged due to its impact on code readability and maintainability.

Syntax:

label:
    // Code to be executed

goto label;

Example:

int i = 0;
loop_start:
    printf("%d\n", i);
    i++;
    if (i < 10) {
        goto loop_start;
    }

In this example, the loop prints the values from 0 to 9, using goto to jump back to the label loop_start.

Difference in while and do..while loop

While loopDo-while loop
Entry level condition checkerExit level condition checker
It will not be executed with out a condition is true at least once.It will be executed at least once before the condition is tested

These are the primary loop control structures in C, each serving a specific purpose and providing flexibility in controlling the flow of the program.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *