If Else

0 luist9776194511 November 13, 2020

If we have to execute a block of statements when a particular condition is met or not met. This is known as Decision making. For decision making, We have 4 types of control statements in C Programming.

  • if statement
  • if….else statement
  • else…..if statement
  • nested if….else statement

1. IF Statement:

If the statement is a powerful decision-making statement and is used to control the flow of execution of statements. It basically a two-way decision statement and is used together with an expression, i.e. test condition. The if statement evaluates the expression first and then, if the value the expression is true, it executes the statement within the block. Otherwise, it skips the statements within its block and continues from the first statement outside the if block. It takes the form,

if(condition)
{
    statement-block;
}
statement-x;

Flow Diagram

Flowchart block of IF Statement
Flowchart block of IF Statement

Let’s take an example to make clear concepts on IF-ELSE.

#include <stdio.h>

int main(){

    int num = 10;

    if(num % 2 == 0){
        printf("Number is Even\n");
    }

    return 0;
}

The output of the above program is:

Number is Even

2. IF…ELSE Statement

Theif…else statement is an extension of the simpleifstatement. It is used when there are two possible actions – one when a condition is true, and the other when it is false. The general form is

if(condition)
{
    true_block statement;
}
else
{
    false_block statement;
}
statement-x;

Flowchart

Flowchart block of IF Statement
Flowchart block of IF Statement

Let’s look upon one example to make clear concepts on if..else condition.

#include <stdio.h>

int main(){

    int num = 10;

    if(num % 2 == 0)
    {
        printf("Number is Even\n");
    }
    else
    {
        printf("Number is Odd\n");
    }

    return 0;
}

The output of the above program is:

Number is Even

3. ELSE…IF Statement

The else if the statement is used when there are more than two possible actions depending upon the outcome of the test. When an action is taken, no others can be executed or taken. In such a situation, the if…else if…else if….else statement is used. This structure takes the form,

if(condition-1)
{
   statement-1;
}
else if(condition-2)
{
   statement-2;
}
else if(condition-3)
{
   statement-3;
}
else
{
    default-statement;
}

Let’s look at one example to make clear concepts on else…if condition.

#include <stdio.h>

int main(){

    int num = 15;

    if(num % 2 == 0)
    {
        printf("Number is Even\n");
    }
    else if(num % 3 == 0)
    {
        printf("Number is Odd\n");
    }
    else
    {
        printf("Invalid Number\n");
    }

    return 0;
}

The output of the above program is:

Number is Odd

4. Nested IF…ELSE Statement

Nested IF…ELSE statement means there is anIF condition inside the if condition. The syntax looks like this.

if(condition)
{
    if(condition-2)
    {
        statement-1;
    }
    else
    {
        statement-2;
    }
}
else
{
    statement-3;
}

Let’s look at examples to make clear concepts on nested if…else condition.

#include  < stdio.h>

int main(){

    int num = 15;

    if(num % 2 == 0)
    {
        if(num > 5 == 0)
        {
            printf("Number is greater than 5 and Even\n");
        }else{
           printf("Number is lessa than 5 andEven\n");
        }
    }
    else
    {
        printf("Number is Odd\n");
    }

    return 0;
}

The output of the above program is:

Number is greater than 5 and Even