Conditional in C Programming - if-else, elseif, switch-case

C programming has a rich history, dating back to the early days of computer science. One of the key features of the C programming language is the ability to use conditional statements to control the flow of a program.

What is if-else

The “if-else” statement is a fundamental building block of C programming, allowing developers to execute different blocks of code based on the outcome of a Boolean expression.

The syntax for an “if-else” statement in C is as follows:

if (expression)
{
    // code to be executed if expression is true
}
else
{
    // code to be executed if expression is false
}

Example

For example, consider a program that checks the value of a variable “x” and prints a message depending on whether “x” is greater than, less than, or equal to 10:

if (x > 10)
{
    printf("x is greater than 10\n");
}
else if (x < 10)
{
    printf("x is less than 10\n");
}
else
{
    printf("x is equal to 10\n");
}

Complete Program of this greater and calculation -

#include <stdio.h>

int main()
{
    int x = 5;

    // check the value of x and print a message
    if (x > 10)
    {
        printf("x is greater than 10\n");
    }
    else if (x < 10)
    {
        printf("x is less than 10\n");
    }
    else
    {
        printf("x is equal to 10\n");
    }

    return 0;
}

In this program, the “if-else” statement checks the value of the variable “x” and prints a message depending on whether “x” is greater than, less than, or equal to 10. The output of this program would be “x is less than 10”.

This is a simple example that demonstrates the basic syntax and usage of an “if-else” statement in C. In a more complex program, the “if-else” statement could be used in combination with other control flow statements and nested within other “if-else” statements to implement more sophisticated logic.

What is Switch

The switch statement is a control structure in C that allows handling multiple conditions. It is useful when dealing with a series of constant values of a variable.

switch (var) {
    case constant1:
        // Code to be executed if expression equals constant1
        break;
    case constant2:
        // Code to be executed if expression equals constant2
        break;
    // Additional cases as needed
    default:
        // Code to be executed if none of the cases match
}

Here, variable var is evaluated, and its value is compared against each case. If a match is found, the block of code is executed. The break statement is crucial to exit the switch block, otherwise it will continue executing subsequent cases. if none of the cases match, the default case is executed.

Example

#include <stdio.h>

int main() {
   
    char ch;
    scanf("%c", &ch);
    
    switch (ch)
    {
    case 'a':
        printf("It's a vowel\n");
        break;
    case 'e':
        printf("It's a vowel\n");
        break;
    case 'i':
        printf("It's a vowel\n");
        break;
    case 'o':
        printf("It's a vowel\n");
        break;
    case 'u':
        printf("It's a vowel\n");
        break;
    default:
        printf("It's a consonant\n");
        break;
    }

    return 0;
}

Here, we take a lowercase letter as input and check whether it’s a vowel or consonant.

Example 2 - Vowel check using if-else

Here is a simple C program that uses an “if-else” statement to check whether a given character is a vowel:

#include <stdio.h>

int main()
{
    char ch = 'a';

    // check if ch is a vowel
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
    {
        printf("%c is a vowel\n", ch);
    }
    else
    {
        printf("%c is not a vowel\n", ch);
    }

    return 0;
}

In this program, the “if-else” statement checks whether the value of the variable “ch” is equal to any of the vowels ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’. If the condition is true, the program prints a message saying that the character is a vowel. Otherwise, it prints a message saying that the character is not a vowel.

The output of this program would be “a is a vowel”. This is a simple example that demonstrates the use of an “if-else” statement to implement a basic conditional check in C programming.

Example 3 - Check if a number is positive or not using c programming

Here is a simple C program that uses an “if-else” statement to check whether a given number is positive:

#include <stdio.h>

int main()
{
    int num = 10;

    // check if num is a positive number
    if (num > 0)
    {
        printf("%d is a positive number\n", num);
    }
    else
    {
        printf("%d is not a positive number\n", num);
    }

    return 0;
}

In this program, the “if-else” statement checks whether the value of the variable “num” is greater than 0. If the condition is true, the program prints a message saying that the number is positive. Otherwise, it prints a message saying that the number is not positive.

The output of this program would be “10 is a positive number”. This is a simple example that demonstrates the use of an “if-else” statement to implement a basic conditional check in C programming.

In conclusion, the “if-else” statement is a fundamental and essential feature of the C programming language. It allows developers to control the flow of a program based on the outcome of a Boolean expression, enabling them to execute different blocks of code depending on whether a certain condition is true or false. The “if-else” statement is versatile, powerful, and widely used in C programming, making it an indispensable tool for any C programmer.

Previous
Input and Output in C Programming
Next
Loops in C Programming - for loop, while loop and do-while loop