if(condition 1) (outside block) { //beginning of (condition 1 block)
if(condition 2) statement 1;
if(condition 3) statement 2;
else statement 3; // this else is for condition 3 (nearest if in the block)
} //end of condition 1 block
else statement 4;// this else is for condition 1 (outside block)
The final else is not associated with if(condition 2) even though it is the closet if without an else, because it is not in the same block. Rather, the final else is associated with if(condition 1). C++ allows at least 256 levels of nesting. In practice you will never need to nest if that many times.
Example
/* This program uses the nested if and modify the previous example*/
#include <iostream.h>
#include <stdlib.h>
main ( )
{
int hope; // number generated by computer randomly
int guess;// number guessed by user
hope=rand(); // assign a random number to hope
cout << "Guess an integer number";
cin >> guess;
if(guess==hope)
{
cout << "Congratulation you are a winner n";
cout << "You have guessed correctly n";
}
else
{
cout << "Sorry, your guess is wrong";
if(guess>hope) cout<<"Your guess is too high n";
else cout <<"Your guess is too low n";
}
return 0;
}