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

 

Suggested for you

If statement in Java

If statement in C++

If statement in C

If statement in Python

 

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.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago