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.
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
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++
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 Python language
Suggested for you
Nested for loop in C++ language
Nested while loop in C++ language
Nested for loop in Java language
Nested while loop in Java 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