Lesson 07·C Programming

Control Statements

Decision making, iterative loops, and jumping statements in C

Decision Making / Conditional Statements

C programs normally execute sequentially. When conditional criteria must be evaluated to alter the flow of execution, C provides decision making statements: if, if-else, nested if-else, and switch-case.

if Statement

Checks a condition first. If true, the enclosed block executes:

C Example
#include <stdio.h>

void main() {
    int a = 5;

    if (a > 4) {
        printf("\nValue of A is greater than 4!");
    }

    if (a == 4) {
        printf("\nValue of A is 4!");
    }
}
Program Output
Value of A is greater than 4!

if-else Statement

Executes the true branch if condition holds, otherwise executes the else block:

C Example
#include <stdio.h>

void main() {
    int no;
    printf("Enter number: ");
    scanf("%d", &no);

    if (no % 2 == 0) {
        printf("\n\nNumber is Even!");
    } else {
        printf("\n\nNumber is Odd!");
    }
}
Program Output
Enter number: 11

Number is Odd!

Nested if-else Statement

Nesting an if-else structure inside another if or else block:

C Example
#include <stdio.h>

void main() {
    int no;
    printf("Enter number: ");
    scanf("%d", &no);

    if (no % 2 == 0) {
        printf("\n\nNumber is Even!");
    } else {
        if (no == 0) {
            printf("\n\nIt is 0!");
        } else {
            printf("\n\nNumber is less than 0 or odd!");
        }
    }
}
Program Output
Enter number: 0

Number is Even!

switch-case Statement

A multi-way branching statement that matches an expression against constant case values to avoid deep nested if ladders:

  • The case label must be an integer or character constant.
  • Each compound case statement should include a break statement to prevent fall-through.
  • Case labels must end with a colon (:).
C Example
#include <stdio.h>

void main() {
    int no;
    printf("Enter any number from 1 to 3: ");
    scanf("%d", &no);

    switch (no) {
        case 1:
            printf("\nIt is 1");
            break;
        case 2:
            printf("\nIt is 2");
            break;
        case 3:
            printf("\nIt is 3");
            break;
        default:
            printf("\nInvalid Number!");
    }
}
Program Output
Enter any number from 1 to 3: 3
It is 3

Enter any number from 1 to 3: 5
Invalid Number!

Looping Statements (Iterative)

A loop executes a block of code repeatedly as long as a test condition evaluates to true.

Entry Controlled Loop

Condition is tested before entering the loop body (e.g., while and for loops). If false initially, body never runs.

Exit Controlled Loop

Loop body executes first, then condition is evaluated (e.g., do-while loop). Always runs at least once.

while Loop (Entry Controlled)

Repeats a block of statements as long as the condition remains true:

C Example
#include <stdio.h>

void main() {
    int a = 1;

    while (a <= 5) {
        printf("Hello\n");
        a += 1; // i.e., a = a + 1
    }
}
Program Output
Hello
Hello
Hello
Hello
Hello

for Loop (Entry Controlled)

Combines initialization, test condition, and step increment in a compact header:

C Example
#include <stdio.h>

void main() {
    int i;

    for (i = 0; i < 5; i++) {
        printf("Hello\n");
    }
}
Program Output
Hello
Hello
Hello
Hello
Hello

do-while Loop (Exit Controlled)

Guarantees that the loop body executes at least once before checking the condition at the end:

C Example
#include <stdio.h>

void main() {
    int a = 1;

    do {
        printf("Hello\n");
        a += 1;
    } while (a <= 5);

    a = 6;
    do {
        printf("Technowell\n");
        a += 1;
    } while (a <= 5);
}
Program Output
Hello
Hello
Hello
Hello
Hello
Technowell

Break Statement

Causes immediate termination of the innermost enclosing loop or switch construct:

C Example
#include <stdio.h>

void main() {
    int i;

    for (i = 1; ; i++) {
        if (i > 5) {
            break;  // Exits the loop when i exceeds 5
        }
        printf("%d ", i);
    }
}
Program Output
1 2 3 4 5

Continue Statement

Skips the remaining statements in the current iteration and jumps directly to the next iteration:

C Example
#include <stdio.h>

void main() {
    int i;

    for (i = 1; i <= 8; i++) {
        if (i == 6) {
            continue;  // 6 is omitted
        }
        printf("%d\n", i);
    }
}
Program Output
1
2
3
4
5
7
8

Goto Statement

Transfers program execution control unconditionally to a specified label. Useful for breaking out of deeply nested loops:

C Example
#include <stdio.h>

void main() {
    int i = 1, j;

    while (i <= 3) {
        for (j = 1; j <= 3; j++) {
            printf("* ");
            if (j == 2) {
                goto stop;  // Jump out of both loops
            }
        }
        i = i + 1;
    }

stop:
    printf("\n\nExited!\n");
}
Program Output
* * 

Exited!