#4 Conditional in C Programming - if-else

Categories - C Programming Tags - Programming   Maniruzzaman Akash   1 month ago   101   3 minutes   0

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.

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
#3 Input and Output in C Programming
Next
#5 Loops in C Programming - for loop, while loop and do-while loop