🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

C if...else Statements

C if...else Statements

Sometimes, we wish to execute one set of instructions if a particular condition is met, and another set of instructions if it is not. This kind of situation is dealt with in the C language using a decision control system.

The condition for the if statement is always enclosed within a pair of parentheses. If the condition is true, then the set of statements following the if statement will execute. If the condition evaluates to false, then the statement will not execute; instead, the program skips that enclosed part of the code.

An expression in if statements is defined using relational operators. Comparing two values using relational operators allows us to determine whether they are equal, unequal, greater than, or less than.

If we want to execute a particular code in some situation and its vice versa, opposite, or different code if that situation doesn’t occur, then if...else statements can be used. It all depends on the condition. If the condition returns a true value, the situation has occurred, and the true part of the code will be executed. If the condition returns a false value, the false part of the code will be executed.

Conditions and Meaning:

Conditions and Meaning
Condition Meaning
a == b a is equal to b
a != b a is not equal to b
a < b a is less than b
a > b a is greater than b
a <= b a is less than or equal to b
a >= b a is greater than or equal to b

The statement written in an if block will execute when the expression following if evaluates to true. But when the if block is followed by an else block, then when the condition written in the if block turns to be false, the set of statements in the else block will execute.

Following is the syntax of if-else statements:

        
if (condition) {
    statements;
} else {
    statements;
}
        
    

One example where we could use the if-else statement is:

        
#include <stdio.h>

int main() {
    int num = 10;
    if (num <= 10) {
        printf("Number is less than equal to 10.");
    } else {
        printf("Number is greater than 10.");
    }
    return 0;
}
        
    

Output:

        
Number is less than equal to 10.
        
    

Ladder if-else:

If we want to check multiple conditions, then ladder if-else can be used. If the previous condition returns false, then only the next condition will be checked.

Syntax:

        
if (/* condition */) {
    // statements
} else if (/* condition */) {
    // statements
} else if (/* condition */) {
    // statements
}
        
    

Nested if-else:

We can also write an entire if-else statement within either the body of another if statement or the body of an else statement. This is called the ‘nesting’ of ifs.

        
if (/* condition */) {
    if (/* condition */) {
        // statements
    } else {
        // statements
    }
} else {
    // statements
}
        
    

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.