nested if

Nested if in Cpp programming language

Nested if in Cpp programming language

We will learn about Nested if in Cpp programming language

In the C++ programming language, use of if statements inside another if block or another else block is called nested if statements.

Nested if statements in C++ language

Declaration

Syntax

The syntax of nested if in C++ language

if(Test_expression){ //outer if
statement(s)
//when the test expression is true
//executes this statements

        if(Test_expression){ //inner if
          statement(s)
        //when the test expression is true
      //executes this statements
}
}


Flow diagram of Nested if statements

Flow diagram – Nestef if in C++

Example

Program 1

#include <iostream>

using namespace std;

int main()
{
    int age=25;
    int marks=56;
    if(age>=18){
    cout << "Age is enough" << endl;
    if (marks>=50){
        cout << "marks is enough" << endl;
        cout << "you can get the job" << endl;
    }

    }
    return 0;
}

 

When we executed above program, it will be produced following result

age is enough
marks is enough
you can get the job
 

Flow diagram of nested if-else in C++

flow diagram of if – else

In the nested-if statements of C++ language, initially flow of control enters the outer loop and boolean expression is evaluated.

When the test expression is true, the body of if statements are executed. Then, the flow of control enters inner if for evaluation of the boolean expression.

If the test expression is false, it executes the codes inside the body of else and exits from the body of if.

 

 

The syntax of nested if – else statements

if(Test_expression1){ //outer if 
statement(s) 
//when the test expression is true 
//executes this statements         
       if(Test_expression2){ //inner if           
         statement(s)         
//when the test expression2 is true       
//executes this statements
         }
       else{
           statement(s) 
           //when the test expression2 is true 
            //executes this statements    
       }
else{
statement(s) 
//when the test expression1 is true 
//executes this statements    
}

 

Program 2

#include <iostream>

using namespace std;

int main()
{
    int iqMarks=70;
    double zScore=0.9;
    if(iqMarks>=60){
        cout << "Your marks is better" << endl;
          if(zScore>=0.8){
        cout << "Your Zsore is enough" << endl;
        cout << "You can enter university" << endl;
          }

        else{
            cout << "your Zscore is not enough" << endl;
        }

}
else{
            cout << "your marks is not better" << endl;
        }
    cout << "End the program" << endl;
    return 0;
}

 

When we execute above program, it will be produced the following result

Your marks is better
Your Z score is enough
Your can enter university
End the program

 

Similar post

If statement in Java language

If statement in C++ language

If statement in C language

If statement in Python language

 

Suggested for you

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

New keyword in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

Two dim Array in C++ language

Three dim Array in C++ language

Single dim Array in Java language

Two dim Array in Java language

Three dim Array in Java language

Single dim Array in C language

Two dim Array in C language

Three dim Array in C language

 

 

Nested if in Python programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago