Nested if in C++ Last Updated : 06 Feb, 2023 Summarize Comments Improve Suggest changes Share 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; } } OutputTrueWhat 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) .......................... .......................... } } 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; } OutputSandeep 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; } Outputgfg 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). Comment More infoAdvertise with us Next Article isgreater() in C/C++ R raj2002 Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2022 CPP-Basics 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 2 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 condit 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 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 Like