Working of the if-else-if ladder

  1. Control falls into the if block.
  2. The flow jumps to Condition 1.
  3. Condition is tested.
    1. If Condition yields true, goto Step 4.
    2. If Condition yields false, goto Step 5.
  4. The present block is executed. Goto Step 7.
  5. The flow jumps to Condition 2.
    1. If Condition yields true, goto step 4.
    2. If Condition yields false, goto Step 6.
  6. The flow jumps to Condition 3.
    1. If Condition yields true, goto step 4.
    2. If Condition yields false, execute else block. Goto Step 7.
  7. Exits the if-else-if ladder.

C++ if else if Ladder

Decision-making in C++ helps to write decision-driven statements and execute a particular set of code based on certain conditions.

In C++, the if-else-if ladder helps the user decide from among multiple options. The C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C++ else-if ladder is bypassed. If none of the conditions is true, then the final statement will be executed.

Similar Reads

Syntax

if (condition) statement 1;else if (condition) statement 2;..else statement;...

Working of the if-else-if ladder

Control falls into the if block. The flow jumps to Condition 1. Condition is tested. If Condition yields true, goto Step 4. If Condition yields false, goto Step 5. The present block is executed. Goto Step 7. The flow jumps to Condition 2. If Condition yields true, goto step 4. If Condition yields false, goto Step 6. The flow jumps to Condition 3. If Condition yields true, goto step 4. If Condition yields false, execute else block. Goto Step 7. Exits the if-else-if ladder....

Flowchart if-else-if ladder

...

Examples of if else if the ladder

Example 1:...