Friday, April 19, 2019

How to Use Nested If Else in C++

What is Nested If Else


The dropping else is a problem in computer programming in which an optional else clause in an if-then(–else) statement results in nested conditionals being ambiguous. Formally, the reference context-free grammar of the language is ambiguous, meaning there is more than one correct parse tree.

In many programming languages one may write conditionally executed code in two forms: the if-then form and the if-then-else form – the else clause is optional:
if a then s
if b then s1 else s2


Program of Nested If Else


#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 int a,b,c,d;
// Process
cout<<"Enter 4 Numbers\n";
cin>>a>>b>>c>>d;

if(a > b){
if (a > c){
if (a > d){
cout<<"Largest no is:- "<<a<<endl;
} else {
cout<<"Largest no is:- "<<d<<endl;
}
} else {
if(c>d){
cout<<"Largest no is:- "<<c<<endl;
} else {
cout<<"Largest no is:- "<<d<<endl;
}
}
} else {
if(b > c) {
if (b > d){
cout<<"Largest no is:- "<<b<<endl;
} else{
cout<<"Largest no is:- "<<d<<endl;
}
} else {
if (c > d){
cout<<"Largest no is:- "<<c<<endl;
} else {
cout<<"Largest no is:- "<<d<<endl;
}
}
}

 return 0;
}

Output


Enter 4 Numbers
5
6
8
1
Largest no is:- 8

No comments:

Post a Comment