Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Java Nested if
Next article icon

Nested if in C++

Last Updated : 06 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

If is a type of condition checking in which a condition turns out to be true a block of statements is executed.

Syntax:

// if base_condition is true // every inside the { } block will be executed if (base_condition) {     statement 1...............     statement 2 .............. }

Example

C++




// C++ Program demonstrate
// use if-else condition
#include <iostream>
using namespace std;
 
int main()
{
    int a = 6, b = 5;
    if (a > b) {
        cout << "True" << endl;
    }
}
 
 
Output
True

What is Nested If?

When a number of if blocks are present one after another with the same scope (the same scope means under one { } block), then that condition is termed as a Nested if condition. If the first condition is True, we go into the next if condition and the subsequent condition is checked until we get a false condition, and the checking stops.

Syntax:

// if base_condition is true control goes to base_condition1 if ( base_condition)  {     // if base_condition is true control goes to base_condition2     if(base_condition1)      {         if(base_condition2)              ..........................         ..........................     } }
Nested if in C++

 

Example 1:

C++




// C++ Program to
// Nested-if conditions
#include <iostream>
using namespace std;
 
int main()
{
 
    int a = 20, b = 10, c = 2;
 
    // if this condition satisfies then
    // control goes to next if condition
    if (a > b) {
        // if this condition also turns out to be
        // true then the statements under
        // this block will get executed
        if (a > c) {
            cout << " a is the largest " << endl;
        }
    }
    return 0;
}
 
 
Output
 a is the largest 

Example 2:

C++




// C++ Program to
// Nested-if conditions
#include <iostream>
using namespace std;
 
int main()
{
 
    int a = 20, b = 10, c = 2;
 
    if (a == 20) {
        if (b == 10) {
            if (c == 2) {
                cout << "Sandeep Sir is Great!!" << endl;
            }
        }
    }
    return 0;
}
 
 
Output
Sandeep Sir is Great!!

Example 3:

C++




// C++ Program to
// Nested-if conditions
#include <iostream>
using namespace std;
 
int main()
{
 
    int a = 20, b = 10, c = 1;
    // this condition is true
    if (a == 20) {
        // this condition is also true
        if (b == 10) {
            // but this condition is false hence
            // we get out of the nested block
            if (c == 2) {
                cout << "Sandeep Sir is Great!!" << endl;
            }
        }
    }
    cout << "gfg\n";
 
    return 0;
}
 
 
Output
gfg

Example 4:

C++




// C++ Program to demonstrate
// Nested-if condition
#include <iostream>
using namespace std;
 
int main()
{
 
    int a = 220, b = 10, c = 1;
    // this condition is itself false we don't
    // get inside the nesting if block
    if (a == 20) {
        if (b == 10) {
            if (c == 2) {
                cout << "Sandeep Sir is Great!!" << endl;
            }
        }
    }
    cout << " No nested if condition is executed \n ";
    return 0;
}
 
 
Output
 No nested if condition is executed   

Time complexity: O(1).

Auxiliary space: O(1).



Next Article
Java Nested if

R

raj2002
Improve
Article Tags :
  • C++
  • Technical Scripter
  • CPP-Basics
  • Technical Scripter 2022
Practice Tags :
  • CPP

Similar Reads

  • Nested Classes in C++
    A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed. For example, p
    1 min read
  • Java Nested if
    Nested if in Java refers to having one if statement inside another if statement. If the outer condition is true the inner conditions are checked and executed accordingly. Nested if condition comes under decision-making statement in Java, enabling multiple branches of execution. Note: Normal if condi
    3 min read
  • std::remove_if in C++ STL
    std::remove_if() is a built-in algorithm of the C++ STL that is used to remove elements from a specified range of elements that satisfies the given condition. The condition can be defined as a function, lambda expression or a function object. It is defined inside the <algorithm> header file. I
    4 min read
  • find() in C++ STL
    C++ find() is a built-in function used to find the first occurrence of an element in the given range. It works with any container that supports iterators, such as arrays, vectors, lists, and more. In this article, we will learn about find() function in C++. [GFGTABS] C++ //Driver Code Starts{ #inclu
    2 min read
  • std::equal() in C++
    std::equal() helps to compares the elements within the range [first_1,last_1) with those within range beginning at first_2. Syntax 1: template bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) first_1, last_1 : Initial and final positions of the first sequence. All the
    2 min read
  • std::search in C++
    std::search is defined in the header file <algorithm> and used to find out the presence of a subsequence satisfying a condition (equality if no such predicate is defined) with respect to another sequence. It searches the sequence [first1, last1) for the first occurrence of the subsequence defi
    4 min read
  • is_sorted() in C++ STL
    In C++, is_sorted() is a built-in function used to check whether the element of the given range is sorted or not in ascending order. In this article, we will learn about is_sorted() function in C++. Let’s take a quick look at a simple example that illustrates is_sorted() method: [GFGTABS] C++ #inclu
    4 min read
  • std::find_first_of in C++
    std::find_first_of is used to compare elements between two containers. It compares all the elements in a range [first1,last1) with the elements in the range [first2,last2), and if any of the elements present in the second range is found in the first one , then it returns an iterator to that element.
    6 min read
  • isgreater() in C/C++
    In C++, isgreater() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions.isgreater() function used to check whether the 1st argument given to the function is greater than the 2nd argument given to the function or not. Mean
    3 min read
  • islessgreater() in C/C++
    In C++, islessgreater() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions.islessgreater() function is used to check whether the 1st argument given to the function is less than or greater than the 2nd argument given to t
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences