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
Similar post
If statement in Python language
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…