Branching and Loop Statements in C Programming

Branching and Loop Control Structures in C

In C programming, branching statements, also known as conditional statements or selection structures, are essential for controlling program execution flow. They enable developers to create programs with multiple execution paths based on different conditions. This article explores the fundamental control structures including if-else, switch, while, for, and do-while constructs.

The if-else Statement

The if statement evaluates a condition and executes associated code when the condition evaluates to true (non-zero). The basic syntax follows this pattern:

  • if(expression) - followed by statement(s)
  • else if(expression) - alternative conditions
  • else - default case when no conditions match

Important considerations:

  • Multiple statements require braces {} for grouping
  • Zero represents false; any non-zero value represents true
  • Use the equality operator == for comparison, not the assignment operator =

Here is a practical example that determines triangle types based on side lengths:

#include <stdio.h>

int main(void) {
    int sideA = 0;
    int sideB = 0;
    int sideC = 0;

    printf("Enter three side lengths: ");
    scanf("%d %d %d", &sideA, &sideB, &sideC);

    /* A valid triangle requires the sum of any two sides
       to be greater than the third side */
    if (sideA + sideB > sideC && sideA + sideC > sideB && sideB + sideC > sideA) {
        /* All three sides equal - equilateral triangle */
        if (sideA == sideB && sideB == sideC) {
            printf("Equilateral triangle\n");
        }
        /* Exactly two sides equal - isosceles triangle */
        else if (sideA == sideB || sideA == sideC || sideB == sideC) {
            printf("Isosceles triangle\n");
        }
        /* No sides equal - scalene triangle */
        else {
            printf("Scalene triangle\n");
        }
    }
    else {
        printf("Not a valid triangle\n");
    }

    return 0;
}

The switch Statement

The switch statement provides a cleaner alternative when comparing a variable against multiple constant values. It evaluates an expression and jumps to the matching case label.

Key rules to remember:

  • case labels must be followed by an integer constant expression
  • break statements prevent fall-through to subsequent cases
  • The default label handles unmatched conditions (typically placed at the end)

Example demonstrating day classification:

#include <stdio.h>

int main(void) {
    int dayNumber = 0;

    printf("Enter day number (1-7): ");
    scanf("%d", &dayNumber);

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

    return 0;
}

The while Loop

The while loop repeatedly executes a block of code as long as its condition remains true. It follows the pattern: initialize, test condition, update counter.

#include <stdio.h>

int main(void) {
    int counter = 0;

    while (counter < 5) {
        printf("Current value: %d\n", counter);
        counter++;
    }

    return 0;
}

This loop prints numbers from 0 to 4, incrementing the counter with each iteration until the condition counter < 5 becomes false.

The for Loop

The for loop consolidates initialization, condition checking, and increment/decrement into a single statement. Its general form is:

for(initialization; condition; increment/decrement) {
    /* loop body */
}

While while and for loops are functionally equivalent, for loops provide better organization when the loop involevs a clear counter variable.

Here's an example that geenrates a multiplication table:

#include <stdio.h>

int main(void) {
    int row = 0;
    int column = 0;

    for (row = 1; row <= 9; row++) {
        for (column = 1; column <= row; column++) {
            printf("%d*%d=%2d  ", row, column, row * column);
        }
        printf("\n");
    }

    return 0;
}

The do-while Loop

The do-while loop differs from while in one crucial aspect: it executes the loop body at least once before evaluating the condition. This makes it suitable for scenarios requiring menu displays or input validation.

#include <stdio.h>

int main(void) {
    int number = 0;

    do {
        printf("Number: %d\n", number);
        number++;
    } while (number < 5);

    return 0;
}

The critical distinction: do-while evaluates its condition after execution, where as while evaluates before.

Tags: C control-flow if-statement switch-statement while-loop

Posted on Fri, 29 May 2026 20:16:15 +0000 by bschmitt78