Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Saturday, 25 June 2011

Nested if statement of java and its uses


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.

No comments:

Post a Comment