Control Flow Statements in C Programming | Codingque

Welcome to C Programming Tutorial

Your guide to getting started with C programming.

Control Flow Statements in C

Conditionals

Conditionals help in making decisions in your code. The common conditionals are:

#include <stdio.h>

int main() {
    int number = 10;

    if (number > 0) {
        printf("The number is positive.\\n");
    } else if (number < 0) {
        printf("The number is negative.\\n");
    } else {
        printf("The number is zero.\\n");
    }

    // Switch example
    switch (number) {
        case 1:
            printf("The number is one.\\n");
            break;
        case 10:
            printf("The number is ten.\\n");
            break;
        default:
            printf("The number is neither one nor ten.\\n");
            break;
    }

    return 0;
}

Code Explanation:

#include <stdio.h> - Includes the Standard Input Output library needed for using printf.

int main() - Defines the main function where execution of the program begins. The integer variable number is initialized with the value 10.

if-else Statements

if (number > 0) - Checks if number is greater than 0. If true, it prints "The number is positive."

else if (number < 0) - Checks if number is less than 0. If true (and the previous condition was false), it prints "The number is negative."

else - Executes if neither of the above conditions is true, implying number is exactly 0. It prints "The number is zero."

switch Statement

switch (number) - Evaluates the value of number and executes the corresponding case block.

case 1: - Checks if number is 1. If true, it prints "The number is one." and exits the switch block due to break.

case 10: - Checks if number is 10. If true, it prints "The number is ten." and exits the switch block.

default: - Executes if number is neither 1 nor 10. It prints "The number is neither one nor ten." and exits the switch block.

return 0; - Returns 0 from the main function, indicating successful execution of the program.

Loops

Loops help in executing a block of code multiple times. The common loops are:

#include <stdio.h>

int main() {
    // For loop
    printf("For loop:\\n");
    for (int i = 0; i < 5; i++) {
        printf("%d\\n", i);
    }

    // While loop
    printf("While loop:\\n");
    int j = 0;
    while (j < 5) {
        printf("%d\\n", j);
        j++;
    }

    // Do-while loop
    printf("Do-while loop:\\n");
    int k = 0;
    do {
        printf("%d\\n", k);
        k++;
    } while (k < 5);

    return 0;
}

Code Explanation:

#include <stdio.h> - Includes the Standard Input Output library needed for using printf.

int main() - Defines the main function where the execution of the program starts.

for Loop

for (int i = 0; i < 5; i++) - Uses a for loop to iterate from 0 to 4 and print each value of i.

Initialization: int i = 0 sets the starting value of i to 0.

Condition: i < 5 checks if i is less than 5. The loop continues as long as this condition is true.

Update: i++ increments i by 1 after each iteration.

Output: printf("%d\\n", i) prints the current value of i, followed by a newline.

while Loop

while (j < 5) - Uses a while loop to iterate from 0 to 4 and print each value of j.

Initialization: int j = 0 sets the starting value of j to 0.

Condition: j < 5 checks if j is less than 5. The loop continues as long as this condition is true.

Output: printf("%d\\n", j) prints the current value of j, followed by a newline.

Update: j++ increments j by 1 after each iteration.

do-while Loop

do { ... } while (k < 5) - Uses a do-while loop to iterate from 0 to 4 and print each value of k.

Initialization: int k = 0 sets the starting value of k to 0.

Loop Body: The block inside do is executed at least once regardless of the condition. printf("%d\\n", k) prints the current value of k, followed by a newline.

Update: k++ increments k by 1 after each iteration.

Condition: while (k < 5) checks if k is less than 5 after executing the loop body. If true, the loop continues; otherwise, it terminates.

return 0; - Returns 0 from the main function, indicating that the program has executed successfully.

Previous Next
Modern Footer