Loading...

Control Statements in C

Control statements in C determine the flow of execution of a program. These statements help in decision-making, looping, and branching.

Types of Control Statements

1. Conditional Statements

Conditional statements allow the program to make decisions based on conditions.

if Statement

Executes a block of code if a specified condition is true.

#include <stdio.h>

int main() {
    int num = 10;
    if (num > 0) {
        printf("Number is positive.");
    }
    return 0;
}

if-else Statement

Executes one block of code if the condition is true and another block if it is false.

#include <stdio.h>

int main() {
    int num = -5;
    if (num > 0) {
        printf("Positive number.");
    } else {
        printf("Negative number.");
    }
    return 0;
}

if-else if-else Statement

Used when multiple conditions need to be checked.

#include <stdio.h>

int main() {
    int num = 0;
    if (num > 0) {
        printf("Positive number.");
    } else if (num < 0) {
        printf("Negative number.");
    } else {
        printf("Zero.");
    }
    return 0;
}

switch Statement

Used when a variable needs to be compared with multiple values.

#include <stdio.h>

int main() {
    int day = 3;
    switch (day) {
        case 1:
            printf("Monday");
            break;
        case 2:
            printf("Tuesday");
            break;
        case 3:
            printf("Wednesday");
            break;
        default:
            printf("Invalid day");
    }
    return 0;
}

2. Looping Statements

Looping statements execute a block of code multiple times.

for Loop

Executes a block of code for a specified number of times.

#include <stdio.h>

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

while Loop

Executes a block of code while a condition is true.

#include <stdio.h>

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

do-while Loop

Executes a block of code at least once before checking the condition.

#include <stdio.h>

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

3. Jump Statements

Jump statements alter the normal flow of execution.

break Statement

Used to exit a loop or switch statement.

#include <stdio.h>

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

continue Statement

Skips the current iteration of a loop and moves to the next iteration.

#include <stdio.h>

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

goto Statement

Transfers control to a labeled statement in the program.

#include <stdio.h>

int main() {
    int num = 5;
    if (num > 0) {
        goto positive;
    }
    printf("This won't print.");
    positive:
    printf("Number is positive.");
    return 0;
}

Control statements are essential for structuring a program's execution. Understanding them is crucial for writing efficient and logical C programs.

If you have any questions, feel free to ask. Thank you for reading!

Thankyou!