Skip to main content

Nested if with flowchart

Short answer

A nested if is an if statement placed inside another if or else. The inner condition is evaluated only when the outer condition is true. The flowchart branches from condition1: if true, evaluate condition2, else execute the outer else block.

Illustrate nested IF statement with its flowchart.

A nested if is an if statement placed inside another if or else. The inner condition is checked only when the outer condition is satisfied.

Syntax:

if (condition1)
{
    if (condition2)
        statement A;
    else
        statement B;
}
else
    statement C;

Flowchart:

       < condition1 ? >
        True |        | False
    < condition2 ? >  [ C ]
     True |    | False   |
       [ A ]  [ B ]      |
         +-----+---------+
               |
          next statement

Example (largest of three):

#include <stdio.h>
int main(void)
{
    int a = 15, b = 9, c = 22;
    if (a > b)
    {
        if (a > c)
            printf("%d is largest\n", a);
        else
            printf("%d is largest\n", c);
    }
    else
    {
        if (b > c)
            printf("%d is largest\n", b);
        else
            printf("%d is largest\n", c);
    }
    return 0;
}

Output:

22 is largest

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 5 questions →