Cpp - Nested if-else Statements

Introduction

Multiple if-else statements can be nested.

An else branch is always associated with the nearest preceding if statement that does not have an else branch.

if( n > 0 ) 
     if( n%2 == 1 ) 
         cout << " Positive odd number "; 
     else 
         cout << "Positive even number"; 

In this example, the else branch belongs to the second if.

You can use a code block to redefine the association of an else branch.

if( n > 0 ) 
{  if( n%2 == 1 ) 
       cout << " Positive odd number \n"; 
} 
else 
   cout << " Negative number or zero\n"; 

Related Topic