Nested if statement in C++.

Nested if statement

  • The if block may be nested in another if or else block. This is called nesting of if or else block.
www.mskuthar.blogspot.com

Syntax of the nested if statement

if(condition 1)

{
  if(condition 2)
  {
    statement(s);
  }
}


if(condition 1)

  statement 1;
else if (condition 2)
  statement2;
else
  statement3;

if-else-if example
if(percentage>=60)
     cout<<"Ist division";
else if(percentage>=50)
     cout<<"IInd division";
else if(percentage>=40)
     cout<<"IIIrd division";
else
     cout<<"Fail" ;

For Example
#include <iostream>
using namespace std;

int main ()
{
   int marks = 55;
   if( marks >= 80) {
      cout << "U are 1st class !!";
   } 
   else {
      if( marks >= 60) {
          cout << "U are 2nd class !!";
      }  
      else {
if( marks >= 40) {
   cout << "U are 3rd class !!";
}
else {  
 cout << "U are fail !!";
        }  
      }
   }
   return 0;
}
Output :
U are 3rd class !!

Post a Comment

Previous Post Next Post