Java (Chapter 2.2) : Nested IF


For any type of consultation, query or doubt. You may contact to the following: (+91) 9804 436 193 debabrataguha20@gmail.com 
and join the group  https://www.facebook.com/groups/331231161093613/



Hope the basic understanding of conditional statement is clear now. But if you are starting your java learning from this chapter only, I will request you to go through 1st 2 chapters and solve the exercises. Then only come to this chapter.

So let’s go to a program which we discussed earlier in the previous chapter ( Starting IF ). It will be like, user will give the month-number as input and program will return the number of days as an output.


Input                    output                 why?

1                            31                          1 means January & there are 31 days in that month
2                            28/29                     2 means February and we can have 28/29 days in Feb
3                            31                          3 means March, that’s why output is 31days
4                            30                          4 means April and April has 30 days

So on …

So, if the month number is m , then we need to start writing the code as per below:

if ( m == 1 )
{
System.out.println ( ” 31 days ” );
}
else
{
System.out.println(” all other months will come here except January”);
}          


Means, all other months except January will fall under the “Else” block. And now we start extending this program a bit..


if ( m == 1 )
{
System.out.println ( ” 31 days ” );
}
else
{
            if( m == 2)
            {
System.out.println(” 28 or 29 days ”);
            }
            else
            {
                        System.out.println ( “ all other months except Jan & Feb “ );
            }
}          

We are placing another IF-ELSE unit inside Parent ELSE block. It means, at first the program is checking if the month is January or not , if yes, then printing “31 days” , otherwise it is again checking if the month is February or not, if “yes” this time, then printing “28/29 days”  and if “no” then going to the child Else block. 

So now inserting some of grand-child IF-ELSE blocks ..

if ( m == 1 )
{
System.out.println ( ” 31 days ” );
}
else
{
            if( m == 2)
            {
System.out.println(” 28 or 29 days ”);
            }
            else
            {
                        if( m == 3)
                        {
System.out.println(” 31 days ”);
                        }
                        else
                        {
                                    if( m == 4)
                                    {
System.out.println(” 30 days ”);
                                    }
                                    else
                                    {
                                                System.out.println ( “ Months except Jan,Feb,March,April “ );
                                    }
                        }
            }

}          


So this is the way to form Nested IF-ELSE program. Some times we call it as IF-ELSE ladder. In the above program , we put the child IF-ELSE block inside the Parent ELSE block. We can nesting the same inside the parent IF block as well. Let us write the above program again in a different way, but this time we will be nesting it inside parent IF block.

if ( m <=12 )
{
            if ( m >= 1 )
            {
                        if ( m>= 2 )
                        {
                                    if ( m >= 3 )
                                    {
                                                if ( m >= 4 )
                                                {
                                                            if ( m >= 5 )
                                                            {
                                                                        // So on .. till month no. 12 
                                                            }
                                                            else
                                                            {
                                                                        System.out.println ( ” 30 days ” );
                                                            }
                                                }
                                                else
                                                {
                                                            System.out.println ( ” 31 days ” );
                                               
                                    }
                                    else
                                    {
                                                System.out.println ( ” 28/29 days ” );
                                    }
                        }
                        else
                        {
                                    System.out.println ( ” 31 days ” );
                        }
}
else
{
                        System.out.println ( ” Invalid month ” );
}
}
else
{
            System.out.println ( ” Invalid month ” );
}          


Hope you got the logic.. I am not explaining much on the above program.. My intention was to show that we can nest anywhere. Even if we can write nested IF-ELSE block inside of both parent IF block and parent ELSE block.




Let’s take another example of conditional statement of a range value. User will input the percentage of attendance and will get the remarks as per the below table:

Percentage of attendance Remarks/Comments
91-100 “Superb !!”
75-89 “Good.. Try to be more frequent”
50-74 “Considering for the last time”
25-49 “ Guardian Call “
<=24 “ No need to give Exam “


import java.util.*;
class chapter2_program4
{
            public static void main (String args[])

{
Scanner sc=new Scanner(System.in);
int att;
System.out.println(“Enter percentage of attendance”);
att=sc.nextInt();
if(att>100)
{
System.out.println(”Invalid attendance”);
}
else
{
            if ( att >= 91)
            {
System.out.println(” Superb !! ”);
                                    }
                                    else
                                    {
                                                if ( att >= 75 )
                                                {
                                                            System.out.println(” Good try to be more frequent”); 
                                                }
                                                else
                                                {
                                                            if ( att >= 50 )
                                                            {
                                                                        System.out.println(” Considering last time”);
                                                            }
                                                            else
                                                            {
                                                                        if ( att >= 25 )
                                                                       
                                                                                    System.out.println(” Guardian Call”);
                                                                        }
                                                                        else
                                                                        {
          System.out.println(”No need to give exam”);
                                                                        }
                                                           
                                                }
                                    }
}
}
}


This is how we can build a strong conditional logic using Nested IF-ELSE block, but let us now analyze what we have written above. We have written the second IF-ELSE block as below:

            if ( att >= 91)

You do not need to write this line like “ if ( att in between 91 and 100 ) “ , because the syntax would be wrong. Another reason is that there is no need to mention the full range. If the control is coming to the parent ELSE block of “ if ( att >= 100) “, that means automatically the value of variable “ att “ is lesser than 100.

Now if the value of “ att “ is greater than 91 ( as per the second IF statement) , means the value of “att “ is in between 91 and 100. By that reason we have not mentioned any range in the program, but still it is giving the desired comments





The last part of this chapter is to show that we can also create this IF-ELSE ladder simultaneously inside of both IF and ELSE block. Let us take the same programming mentioned above.

if ( att > 100 )
{
System.out.println(”Invalid attendance”);
}
else
{
            if ( att >=50 )
            {
                        if ( att >= 91)
{
System.out.println(” Superb !! ”);
                        }
                        else
                        {
                                    if ( att >= 75 )
                                    {
                                                System.out.println(” Good try to be more frequent”); 
                                    }
                                    else
                                    {
                                                System.out.println(” Considering last time”);
                                    }
                        }
            }
            else
            {
                        if ( att >= 25 )
                       
                                    System.out.println(” Guardian Call”);
                        }
                        else
                        {
System.out.println(”No need to give exam”);
                        }
}
}

Hope you are now able to go and solve the exercise mentioned in the Facebook Group Album (mentioned at the starting of this chapter) for Basic Programming, “chapter 2.2 Nested IF”. But keep in mind do not use && or || or anything else which we have not learned yet. You can contact me over my email if you have any concern or doubt. You can also comment here.

Best of Luck ..

Comments

Popular posts from this blog

Java concepts Theory