Nested if:
It can be defined as an if statement containing another if statement with in its body/block. When we have to make one decision from multiple decisions we use nested if. A nested if is an if statement that is the target of another if or else. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else.
General syntax:
if (condition)
{
if (condition)
if (condition) /* this if is associated with */
statement / block
else /* this else*/
statement / block
}
else /*and this else is associated with the most first if*/
statement / block
Here, in the block of if the second if in it is associated with the first if within the block. Remember an else always refers to the nearest if without an else and within the same block.
- Click here to see an example