Control Structures in C: Branching and Looping

Conditional Execution with if and switch

The C language provides several constructs for implementing decision-making logic. The if statement allows execution of code based on a condition. When the condition evaluates to true (non-zero), the associated block runs. Otherwise, it is skipped. An else clause can be added to handle the false case.


#include <stdio.h>
int main() {
    int value = 0;
    scanf("%d", &value);
    if (value % 2 == 1) {
        printf("%d is odd\n", value);
    } else {
        printf("%d is even\n", value);
    }
    return 0;
}

Multiple statements within if or else blocks must be enclosed in curly braces to ensure they are treated as a single logical block. Without braces, only the first statement is conditionally executed.

Nested if and Dangling-else Problem

Nested if statements can lead to ambiguity known as the "dangling-else" problem. In such cases, a else clause always binds to the nearest unmatched if. Proper use of braces avoids confusion and improves code clarity.


#include <stdio.h>
int main() {
    int a = 0, b = 2;
    if (a == 1) {
        if (b == 2) {
            printf("hehe\n");
        }
    } else {
        printf("haha\n");
    }
    return 0;
}

Using switch for Multiple Conditions

The switch statement provides a cleaner alternative for handling multiple discrete values. It requires an integer expression and matching case labels. The default case handles unmatched values.


int day = 0;
scanf("%d", &day);

switch (day) {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        printf("Workday\n");
        break;
    case 6:
    case 7:
        printf("Weekend\n");
        break;
    default:
        printf("Invalid input\n");
        break;
}

The break statement is crucial to prevent fall-through behavior, where execution continues into subsequent case blocks. Omitting break is intentional in some advanced use cases.

Looping Constructs

C provides three primary looping mechanisms: while, for, and do-while. These structures alow repeated execution of code blocks based on specific conditions.

while Loop

The while loop checks the condition before each iteration. If the condition is false initially, the loop body does not execute.


#include <stdio.h>
int main() {
    int i = 1;
    while (i <= 10) {
        printf("%d ", i);
        i++;
    }
    return 0;
}

for Loop

The for loop is the most commonly used for iteration. It combines initialization, condition check, and update into a single line.


#include <stdio.h>
int main() {
    for (int i = 1; i <= 10; i++) {
        printf("%d ", i);
    }
    return 0;
}

do-while Loop

The do-while loop guarantees at least one execution of the loop body since the condition is checked after the first iteration.


#include <stdio.h>
int main() {
    int i = 1;
    do {
        printf("%d ", i);
        i++;
    } while (i <= 10);
    return 0;
}

Control Flow Modifiers: break and continue

The break statement exits the loop immediately, while continue skips the rest of the current iteration and proceeds to the next one. These statements provide fine-grained control over loop execution.

Tags: C Control Structures Branching looping if

Posted on Wed, 13 May 2026 14:03:57 +0000 by Eal