C Conditional Statements

Conditional statements in C allow a program to make decisions based on certain conditions. These statements control the flow of execution by checking if a specific condition is met. If the condition is true, a certain block of code executes; otherwise, another block runs.

In this tutorial, we will learn about different conditional statements in C with detailed explanations and examples.


Types of Conditional Statements in C

C provides the following conditional statements:

  1. if statement – Executes a block of code if a condition is true.
  2. if-else statement – Executes one block if the condition is true, otherwise executes another block.
  3. if-else if-else statement – Allows multiple conditions to be checked sequentially.
  4. switch statement – Allows a variable to be tested against multiple values.

1. The if Statement

The if statement checks a condition, and if it evaluates to true, the code inside the block executes.

Syntax:

</>
Copy
if (condition) {
    // Code to execute if condition is true
}

Example: Check if a number is positive

In this example program, we will check if the value in variable num is a positive number using if statement.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        printf("The number is positive.\n");
    }

    return 0;
}

Explanation:

  1. The variable num is initialized with 10.
  2. The if statement checks if num > 0.
  3. Since the condition is true, the message "The number is positive." is printed.

Output:

The number is positive.

Detailed tutorial on if statement.


2. The if-else Statement

The if-else statement executes one block of code if the condition is true, and another block if the condition is false.

Syntax:

</>
Copy
if (condition) {
    // Code executes if condition is true
} else {
    // Code executes if condition is false
}

Example: Check if a number is even or odd

In this example program, we will check if the value in variable num is an even number or odd number using if-else statement.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 7;

    if (num % 2 == 0) {
        printf("The number is even.\n");
    } else {
        printf("The number is odd.\n");
    }

    return 0;
}

Explanation:

  1. The variable num is assigned 7.
  2. The condition num % 2 == 0 checks if the number is even.
  3. Since 7 is odd, the else block executes, printing "The number is odd.".

Output:

Detailed tutorial on if-else statement.

The number is odd.

3. The switch Statement

The switch statement is used when a variable needs to be compared against multiple values.

Syntax:

</>
Copy
switch (variable) {
    case value1:
        // Code executes if variable == value1
        break;
    case value2:
        // Code executes if variable == value2
        break;
    default:
        // Code executes if no case matches
}

Example: Display a day based on user input

In this example program, we will print the name of day based on the value in variable day using switch statement.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int day = 3;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        default:
            printf("Invalid day\n");
    }

    return 0;
}

Explanation:

  1. The variable day is assigned 3.
  2. The switch statement compares day with cases.
  3. Since day is 3, “Wednesday” is printed.

Output:

Wednesday

Detailed tutorial on switch statement.