Sunday 2 August 2015

Nested if Structure :


if statement within an if statement is called nested if statement. In nested structure, control enters into the inner if only when the outer condition is true. Only one block is executed and the remaining blocks is skipped automatically.

Syntax

The syntax of nested if is as follows :

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

           else {
               statement(s);
                   }       

Red text is Inner if and black text is Outer if .  

Working : 

In nested if statement, the condition of outer if is checked. If it is true, the control enters in the inner if block. The inner if is working according to simple if statement. If the outer if condition is false, the inner if is skipped and control directly moves to the else part of outer if.    

Nested if Flowchart :


                lets analyze about syntax and flowchart nested if else syntax


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 !!

Saturday 1 August 2015

Multiple if-else-if Statments :


if else if statement can be used to choose one block of statements from many blocks statements. It is used when there are many options but only one block can be executed on the basis of a condition. 

Syntax 


The syntax is as follows :

                if (condition)
                      block 1;
               else
               if (condition)
                     block 2;
               else 
               if (condition)
                     block 3;
                .
                .
                .
               else
                    block n;

Working :


When the first condition becomes true then first block gets executed.

When first condition becomes false then second block is checked against the condition. If second condition becomes true then second block gets executed.

if else if Flowchat :

Else if ladder

Example :

#include <iostream>
using namespace std;
 
int main ()
{
   // declare local variable
   int marks = 55;
 
   // check the condition
   if( marks >= 80 )
   {
       // if 1st condition is true
       cout << "U are 1st class !!" << endl;
   }
   else if( marks >= 60)
   {
       // if 2nd condition is false
       cout << "U are 2nd class !!" << endl;
   }
   else if( marks >= 40)
   {
       // if 3rd condition is false
       cout << "U are 3rd class !!" << endl;
   }
   else 
   {
       // none of condition is true
       cout << "U are fail !!" << endl;
   }
 
   return 0;
}

Output :

U are 3rd class !!