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 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. And 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 are 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 and if the condition returns a false value false part of the code will be executed

 

Conditions

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 the multiple conditions then the ladder if else can use. If the previous condition returns false then only the next condition will be checked.

syntax-

if(/*conditon*/)
{
//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
}

 

Previous Post Next Post