Discuss different types of if statements with example of each. Differentiate if statement with switch statement.

This answer is restricted. Please login to view the answer of this question.

Login Now

There are 4 different types of if 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,

Syntax:

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

Example

#include <stdio.h>

int main(){

    int num = 10;

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

    return 0;
}

The output of above program is

Number is Even 

2. IF…ELSE Statement

The if…else statement is an extension of the simple if statement. 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;

Example:

#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 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;
}

Example:

#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 above program is

Number is Odd

4. Nested IF…ELSE Statement

Nested IF…ELSE statement means there is an IF 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 above program is

Number is greater than 5 and Even

Source: https://codehelppro.com/detail/c-programming/if-else/

The Difference between if and switch statement

Basis If-else switch
Definition Depending on the condition in the ‘if’ statement, ‘if’ and ‘else’ blocks are executed. The user will decide which statement is to be executed.
Expression It contains either logical or equality expression. It contains a single expression which can be either a character or integer variable.
Evaluation It evaluates all types of data, such as integer, floating-point, character or Boolean. It evaluates either an integer, or character.
Sequence of execution First, the condition is checked. If the condition is true then ‘if’ block is executed otherwise ‘else’ block It executes one case after another till the break keyword is not found, or the default statement is executed.
Default execution If the condition is not true, then by default, else block will be executed. If the value does not match with any case, then by default, default statement is executed.
Editing Editing is not easy in the ‘if-else’ statement. Cases in a switch statement are easy to maintain and modify. Therefore, we can say that the removal or editing of any case will not interrupt the execution of other cases.
Speed If there are multiple choices implemented through ‘if-else’, then the speed of the execution will be slow. If we have multiple choices then the switch statement is the best option as the speed of the execution will be much higher than ‘if-else’.
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .