Open In App

C++ if else if Ladder

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax

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

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.

Flowchart if-else-if ladder

if-else-if-ladder

Examples of if else if the ladder

Example 1:

Below program illustrates the use if else if ladder in C++.

C++




// C++ program to illustrate if-else-if ladder 
#include <iostream> 
using namespace std; 
    
int main() 
    int i = 20; 
    
    // Check if i is 10 
    if (i == 10) 
        cout << "i is 10"
    
    // Since i is not 10 
    // Check if i is 15 
    else if (i == 15) 
        cout << "i is 15"
    
    // Since i is not 15 
    // Check if i is 20 
    else if (i == 20) 
        cout << "i is 20"
    
    // If none of the above conditions is true 
    // Then execute the else statement 
    else
        cout << "i is not present"
    
    return 0; 
}


Output

i is 20

Explanation:

  • Program starts.
  • i is initialized to 20.
  • condition 1 is checked. 20 == 10, yields false.
  • condition 2 is checked. 20 == 15, yields false.
  • condition 3 is checked. 20 == 20, yields true. “i is 20” gets printed.
  • “Outside if-else-if” gets printed.
  • Program ends.

Example 2:

Another example to illustrate the use of if else if ladder in C++.

C++




// C++ program to illustrate if-else-if ladder
  
#include <iostream>
using namespace std;
  
int main()
{
    int i = 25;
  
    // Check if i is between 0 and 10
    if (i >= 0 && i <= 10)
        cout << "i is between 0 and 10" << endl;
  
    // Since i is not between 0 and 10
    // Check if i is between 11 and 15
    else if (i >= 11 && i <= 15)
        cout << "i is between 11 and 15" << endl;
  
    // Since i is not between 11 and 15
    // Check if i is between 16 and 20
    else if (i >= 16 && i <= 20)
        cout << "i is between 16 and 20" << endl;
  
    // Since i is not between 0 and 20
    // It means i is greater than 20
    else
        cout << "i is greater than 20" << endl;
}


Output

i is greater than 20


Previous Article
Next Article

Similar Reads

C++17 new feature : If Else and Switch Statements with initializers
In many cases, we need to check the value of something returned by a function and perform conditional operations based on this value. So our code looks something like this // Some function return_type foo(params) // Call function with params and // store return in var auto var = foo(params); if (var == /* some value */) { //Do Something } else { //
3 min read
Find number formed in K steps by reducing N by 1 if last digit is 0 else divide by 10
Given two integers N and K. Perform the following type of operations on N: if the last digit of N is non-zero, decrease the number by one.if the last digit of N is zero, divide the number by 10 (i.e. remove the last digit). The task is to print the result after K such operations. Examples: Input: N = 512, K = 4Output: 50Explanation: Following are t
4 min read
C++ Nested if-else Statement
In C++, there are various types of conditional statements such as if, if-else, nested if, nested if-else, and switch. In this article, we will learn about the nested if-else statement. What are Nested if-else Statements?Nested if-else statements are those statements in which there is an if statement inside another if else. We use nested if-else sta
3 min read
Execute both if and else statements in C/C++ simultaneously
Write a C/C++ program that executes both if-else block statements simultaneously. Syntax of if-else statement in C/C++ language is: if (Boolean expression) { // Statement will execute only // if Boolean expression is true } else { // Statement will execute only if // the Boolean expression is false } Hence we can conclude that only one of the block
2 min read
switch vs if else
Prerequisite - Switch Statement, Decision making(if else) A switch statement is usually more efficient than a set of nested ifs. Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing. Check the Testing Expression: An if-then-else statement can test expressions
3 min read
C++ if else Statement
Decision Making in C++ helps to write decision-driven statements and execute a particular set of code based on certain conditions. The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes th
2 min read
Difference Between if-else and switch in C
In C programming both switch statements and if-else statements are used to perform decision-making and control the flow of the program according to predefined conditions. In this article, we will discuss the differences between the if-else and switch statements. switch StatementA control flow statement called a switch statement enables a program to
4 min read
Article Tags :
Practice Tags :