Nested if in C++

 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++ Program demonstrate
// use if-else condition
#include <iostream>
using namespace std;
 
int main()
{
    int a = 7, b = 6;
    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) 
            ..........................
        ..........................
    }
}


Example 1:

// 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++ 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++ 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++ 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).


Winter-time is here and so is the time to skill-up! More than 5,000 learners have now completed their journey from basics of DSA to advanced level development programs such as Full-Stack, Backend Development, Data Science. 

And why go anywhere else when our DSA to Development: Coding Guide will help you master all this in a few months! Apply now to our DSA to Development Program and our counsellors will connect with you for further guidance & support.


Comments

🎮 POPÜLER ⭐ 4.5

Ultra Blocks

Klasik Bulmaca · Strateji · Zeka Oyunu

Hızlı düşün, akıllıca istifle ve küresel liderlik tablosunda zirveye oyna! Ultra Blocks, klasik arcade bulmaca mantığını modern strateji ve göz alıcı görsel efektlerle birleştiriyor. Blokları sürükleyip yerleştir, tam satırları temizle ve rekorlarını kır!

🏆 Küresel Sıralama PlayFab ile dünyaya meydan oku
🎨 Özelleştirilebilir Tema Neon’dan klasik ahşaba
📱 Her Cihazda Telefon, tablet, TV ve katlanabilir
👑 VIP Kulüp Reklamsız · Sınırsız döndürme
⬇️ Google Play'den Ücretsiz İndir
📦 50 MB · Çevrimdışı oynanabilir 🔒 Veriler şifrelenir

Popular posts from this blog

Kotlin Hello World

Kotlin Android Program (QCR) Application Codes That Read Text in Photos

Kotlin Google Sign in Activity with Layout Example