QBASIC (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 QBASIC 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 ) THEN
PRINT “ 31 DAYS “
ELSE 
PRINT “ ALL OTHER MONTHS WILL COME HERE EXCEPT JANUARY
END IF

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

IF  ( M = 1 ) THEN  
PRINT “ 31 DAYS “
ELSE
IF  ( M = 2 ) THEN
PRINT “ 28 OR 29 DAYS “ 
ELSE 
                        PRINT  “ ALL OTHER MONTHS EXCEPT JAN & FEB “ 
            END IF
END IF


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 ) THEN  
PRINT “ 31 DAYS “
ELSE
IF  ( M = 2 ) THEN
PRINT “ 28 OR 29 DAYS “ 
ELSE
            IF ( M = 3 ) THEN
                        PRINT “ 31 DAYS “
            ELSE
                        IF ( M = 4 ) THEN
                                    PRINT “ 30 DAYS “
                        ELSE
                                                PRINT  “ ALL OTHER MONTHS EXCEPT JAN,FEB,MAR,APR"
                                    END IF
                        END IF 
            END IF
END IF


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  ) THEN
            IF  ( M >= 1 ) THEN    
                        IF  ( M>= 2 ) THEN   
                                    IF  ( M >= 3 ) THEN    
                                                IF  ( M >= 4 ) THEN    
                                                            IF  ( M >= 5 ) THEN    
                                                                        PRINT  “ SO ON .. TILL MONTH NO. 12” 
                                                            ELSE 
                                                                        PRINT “ 30 DAYS “ 
                                                            END IF
                                                ELSE 
                                                            PRINT “ 31 DAYS “
                                                END IF
                                    ELSE 
                                                PRINT “ 28/29 DAYS “
                                    END IF 
                        ELSE 
                                    PRINT “ 31 DAYS “ 
                        END IF
ELSE 
                        PRINT “ INVALID MONTH “ 
            END IF
ELSE 
            PRINT “ INVALID MONTH “ 
END IF

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-90 “Good.. Try to be more frequent”
50-74 “Considering for the last time”
25-49 “ Guardian Call “
<=24 “ No need to give Exam “


INPUT “ ENTER PERCENTAGE OF ATTENDANCE “ ; ATT

IF ( ATT > 100 ) THEN 
PRINT “ INVALID ATTENDANCE “ 
ELSE
IF ( ATT >= 91 ) THEN 
PRINT “ SUPERB !! “ 
ELSE
IF  ( ATT >= 75 ) THEN 
PRINT “ GOOD TRY TO BE MORE FREQUENT “  
ELSE
IF ( ATT >= 50 ) THEN
PRINT “ CONSIDERING LAST TIME “ 
ELSE
IF  ( ATT >= 25 ) THEN  
PRINT “ GUARDIAN CALL “ 
ELSE 
PRINT “ NO NEED TO GIVE EXAM “ 
                                                END IF
                                    END IF
                        END IF
ENDIF
END IF



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 ) THEN  

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 output


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 ) THEN 
PRINT ” INVALID ATTENDANCE “ 
ELSE
IF (  ATT >=50 ) THEN
                        IF  ( ATT >= 91 ) THEN 
PRINT ” SUPERB !! “ 
                        ELSE
IF  ( ATT >= 75 ) THEN
                                                PRINT ” GOOD TRY TO BE MORE FREQUENT “  
                                    ELSE 
                                                PRINT ” CONSIDERING LAST TIME “ 
                                    END IF
                        END IF
ELSE 
                        IF  ( ATT >= 25 )  THEN
                                    PRINT” GUARDIAN CALL “ 
                        ELSE 
PRINT” NO NEED TO GIVE EXAM “ 
                        END IF
            END IF
END IF


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.



Please leave your comments here in the comment box. Give your feedbacks with the good parts of this material or suggest where exactly you are still facing difficulties.









Comments

Popular posts from this blog

Java concepts Theory

About Myself ..