Nested if else — Flow Control

Nested if-else – Easy if and if...else...if statements present an amazing help to manage applications circulation. Easy if is single situation primarily based job i.e. “if some situation is true, then do the duty. In distinction if...else...if assertion offers a number of situation checks i.e. “if some situation is true, then do some job. The situation is fake, then examine another situation and do some job. If all situations fail, then do some default job.”

Learn extra

Take into account a state of affairs, the place you need to execute an announcement primarily based on a number of ranges of situation examine. For instance – At the airport, there are multi-levels of checking earlier than boarding. First, you go for primary safety examination, then ticket examination. You probably have a legitimate ticket, you then go for passport examination. You probably have a legitimate passport, then once more you’ll go for a safety examination. If all these steps are accomplished efficiently, then solely you may board in any other case actions are taken by the authority.

Nested if...else statements have a means to manage program circulation primarily based on a number of ranges of situations.

Syntax of nested if...else assertion

if (boolean_expression_1)
{
    if(nested_expression_1)
    
    else
    
        // If boolean_expression_1 is true
        // however nested_expression_1 is fake
     
    // If boolean_expression_1 is true
}
else
{
    if(nested_expression_2)
    
    else
    
        // If each boolean_expression_1 and
        // nested_expression_2 is fake
     

    // If boolean_expression_1 is fake
}

Be aware: Within the above syntax, I’ve nested one if...else assertion inside one other. Nonetheless, any two decision statements might be inside different.

Nested if else -- Teche Programmer
Flowchart of nested if...else assertion

Instance program of nested if...else assertion

Allow us to write a program to enter three numbers from person. Discover most between given three numbers.

/**
 * C program to seek out most between three numbers
 */

#embody <stdio.h>

int important()
{
    /* Declare three integer variables */
    int num1, num2, num3;

    /* Enter three numbers from person */
    printf("Enter three numbers: ");
    scanf("%dpercentdpercentd", &num1, &num2, &num3);

    if(num1 > num2)
    {
        if(num1 > num3)
        {
            /* If num1>num2 and num1>num3 */
            printf("Num1 is max.");
        }
        else
        
    }
    else
    {
        if(num2 > num3)
        {
            /* If num1<num2 and num2>num3 */
            printf("Num2 is max.");
        }
        else
        {
            /* If num1<num2 and num2<num3 */
            printf("Num3 is max.");
        }
    }

    return 0;
}

Output of the above program

Enter three numbers: 10
20
30
Num3 is max.

Allow us to now perceive the working circulation of above program.

  • I believe you have to be clear upto scanf() assertion. In any other case learn intimately about basic input/output in C.
  • Suppose person inputs three numbers as num1=10num2=20, and num3=30.
  • The primary outer if situation if(num1 > num2) is false since 10 > 20 is false. Therefore, outer if assertion is skipped, executing the outer else half.
  • Contained in the outer else, situation if(num2 > num3) can be false, since 20 > 30 is false. Therefore, the internal if assertion is skipped, executing internal else half.
  • Contained in the internal else there's nothing a lot to do. Only an easy printf() assertion, printing “Num3 is max.”

Earlier than shifting to the subsequent tutorial, should attempt some workouts primarily based on if...else assertion.

Leave a Reply

Your email address will not be published. Required fields are marked *