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.
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 } }
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
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.
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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…