Open In App

C++ Nested if-else Statement

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 statements when we want to implement multilayer conditions(condition inside the condition inside the condition and so on). C++ allows any number of nesting levels.

Basic Syntax of Nested if-else

if(condition1)
{
// Code to be executed
if(condition2)
{
// Code to be executed
}
else
{
// Code to be executed
}
}
else
{
// code to be executed
}

In the above syntax of nested if-else statements, the inner if-else statement is executed only if ‘condition1’ becomes true otherwise else statement will be executed and this same rule will be applicable for inner if-else statements.

Flowchart of Nested if-else

flow diagram of nested if-else

Flow Diagram of Nested if-else

Examples of Nested if-else

The following C++ programs demonstrate the use of nested if-else statements.

Example 1: Check the greatest of three numbers

In the below example, we will find the greatest number out of the three given numbers using nested if-else statements.

Algorithm

Assume that a, b, and c are three given numbers:

  1. If ( a < b ) evaluates to true, then
    1. If ( c < b ) is true, then b is greatest.
    2. else c is greatest.
  2. If ( a < b ) evaluates to false, then
    1. If ( c < a ) is true, then a is greatest.
    2. else c is greatest.

Implementation in C++

C++




// C++ Program to evaluate largest of the three numbers
// using nested if else
#include <iostream>
using namespace std;
  
int main()
{
    // declaring three numbers
    int a = 10;
    int b = 2;
    int c = 6;
  
    // outermost if else
    if (a < b) {
        // nested if else
        if (c < b) {
            printf("%d is the greatest", b);
        }
        else {
            printf("%d is the greatest", c);
        }
    }
    else {
        // nested if else
        if (c < a) {
            printf("%d is the greatest", a);
        }
        else {
            printf("%d is the greatest", c);
        }
    }
  
    return 0;
}


Output

10 is the greatest



Example 2: Checking leap year using nested if-else

A year is a leap year if it follows the condition, the year should be evenly divisible by 4 then the year should be evenly divisible by 100 then lastly year should evenly divisible by 400 otherwise the year is not a leap year. 

Algorithm

  1. If the year is divisible by 4,
    1. If the year is divisible by 100,
      1. If the year is divisible by 400, the year is a leap year
      2. else the year is not a leap year.
    2. else the year is a leap year
  2. else, the year is not a leap year.

Implementation in C++

C++




// C++ program to check leap year using if-else
#include <iostream>
using namespace std;
  
int main()
{
    int year = 2023;
  
    if (year % 4 == 0) {
        // first nested if-else statement
        if (year % 100 == 0) {
            // second nested if-else statement
            if (year % 400 == 0) {
                cout << year << " is a leap year.";
            }
            else {
                cout << year << " is not a leap year.";
            }
        }
        else {
            cout << year << " is a leap year.";
        }
    }
    else {
        cout << year << " is not a leap year.";
    }
  
    return 0;
}


Output

2023 is not a leap year.



If the input year is 2023:

Output

2023 is not a leap year.



If the input year is 2024:

Output

2024 is a leap year.





Previous Article
Next Article

Similar Reads

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
Nested switch statement in C++
Switch-case statements: These are a substitute for long if statements that compare a variable to several integral values The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.Switch is a control statement that allows a value to change contro
2 min read
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
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
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 associate
3 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
Print "Even" or "Odd" without using conditional statement
Write a program that accepts a number from the user and prints "Even" if the entered number is even and prints "Odd" if the number is odd. You are not allowed to use any comparison (==, &lt;,&gt;,...etc) or conditional statements (if, else, switch, ternary operator,. Etc). Method 1 Below is a tricky code can be used to print "Even" or "Odd" accordi
4 min read
C++ Break Statement
The break in C++ is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there and control returns from the loop immediately to the first statement after the loop. Syntax: break; Basically, break statements are used in situations when we are not sure
5 min read
goto Statement in C
The C goto statement is a jump statement which is sometimes also referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. Syntax: Syntax1 | Syntax2 ---------------------------- goto label; | label: . | . . | . . | . label: | goto label; In the above syntax, the first line te
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg