Friday 3 January 2014

Nested if Statement in Java

The most common Control Statements in Java

Control Statement Nested if


The nested if Statement:

Nested if are very common in programming. Nesting an if  Statement just means putting one if Statement inside of another. when you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is with  in the same block as the else and that is not already associated with an else.


Let’s look at syntax of if statement:


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


Example for simple if statement

if(p==10)
{
if(q<20)
i=j;  
if(r>100)
a=b;   //this if is
else    // associated with this else
i=a;
}
else
{
i=b;    //this else refers to if(p==10)
}



Control flow of nested if statement



Nested if Statement Control Flow Diagram


No comments :

Post a Comment