QBASIC (Chapter 2.3) : Composite 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/ 


Let’s take the “month-number” to “count of days” example once again like previous 2 chapters. If you missed that, I will suggest you to go through those 2 chapters first and then come here. Anyway the program was like below, user would enter “month-number” as input and he/she will get the number of days as 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 the solution of this was something like below after the implementation of Nested-IF ..

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



So this program is long enough, that we can not write fully here. Here we have written the code upto month “April” only. To make it short we can introduce some shortcuts to combine all the conditions. This shortcut can be applied by “AND” and “OR”.

“AND” is called as “AND Operator”. It means if all the conditions are TRUE, then only the IF-statement will be declared as “TRUE”.

“OR” is called as “OR operator”. It means if any of the conditions are TRUE inside the parent IF statement, the combined statement will be declared as “TRUE”.

Let’s directly jump into the coding to make it more clear.. We will now try to rewrite the previous program again with combining the conditions HAVING COMMON OUTPUT. 

IF ( M > 12 OR M < 1 ) THEN 

PRINT   “ INVALID MONTH 
ELSE
IF ( M = 1  OR M = 3 OR M = 5 OR M = 7 OR M = 8 OR M = 10 OR M = 12 ) THEN
                        PRINT   “ 31 DAYS 
ELSE
IF ( M = 2 ) THEN
                                    PRINT   “ 30 DAYS   
ELSE               
                                    PRINT   “ 28/29 DAYS 
                        END IF
            END IF
END IF

            
That means if the month is January or the month is March or the month is May  or so on.. the output will be 31.. Again focus on my statement, specially on the word “OR” because a single month input can not be January And March And May.. It can be 1 value at a time. So “OR” is the best operator to be used here to combine the conditions having common output.

So let us analyze the above program in more details.

Say if the input is 3 in the above program.. means M = 3,

Then the combined answer of IF M = 1  OR M = 3 OR M = 5 OR M = 7 OR M = 8 OR M = 10 OR M = 12   will return TRUE as one of its child condition is returning TRUE.

Say if the input is 4 in the above program.. means M = 4,

Then the combined answer of IF M = 1  OR M = 3 OR M = 5 OR M = 7 OR M = 8 OR M = 10 OR M = 12  will return FALSE as no child condition is returning TRUE.

Let’s take another example like below



Question version 1.

User will get admission in a school next year if he/she has 80% attendance and 70% marks in practical and  60% marks in theory.

Solution

INPUT  “ ENTER PERCENTAGE OF ATTENDANCE “ ; ATT
INPUT  “ ENTER MARKS IN PRACTICAL    ; PM
INPUT  “ ENTER MARKS IN THEORY  “ ; TM

IF ( ATT >=  80 AND PM >= 70 AND TM >= 60 ) THEN  
PRINT  “ YOU WILL GET ADMISSION 
ELSE 
PRINT  “ YOU WILL NOT GET ADMISSION 
END IF
END


Note It is to be noted that here to get admission , students need to satisfy all the underlying conditions, that is why “AND” operator is used. If all the sub-conditions return TRUE, then only the Parent condition will return TRUE. Missing any of them will lead to ELSE  statement.





Question version 2.

Say Government is making rules easier. Now anyone will get admission in a school if he/she has any of the following,
  •        80% attendance
  •        70% marks in practical
  •        60% marks in theory.
Solution (skipping the input part)

IF ( ATT >=  80 OR PM >= 70 OR TM >= 60 ) THEN 
PRINT  “ YOU WILL GET ADMISSION 
ELSE 
PRINT  “ YOU WILL NOT GET ADMISSION 
END IF


Note It is to be noted that to get admission , students need to satisfy any one underlying condition only, that is why “OR” operator is used. If any sub-condition returns TRUE, then also the Parent condition will return TRUE. Missing all of them only will lead to ELSE  statement.

Another thing here to be highlighted that a good student who satisfies all the conditions will also get admission. Try to differentiate between the word “Atleast one condiotion” and “Exactly one condition”. As you have more knowledge on English Language than me, it would be easier for you to get the idea of “Atleast one”. So the student who will satisfy all the conditions will also get admitted.




Question version 3.

Government is now making rules in more trickier way. Now anyone will get admission in a school if he/she has 80% attendance. For the students having lesser attendance will get another chance if they will have marks more than 70% practical and 60% in theory

Solution

IF ( ATT >=  80 OR ( PM >= 70 AND TM >= 60 ) ) 
PRINT  “ YOU WILL GET ADMISSION 
ELSE 
PRINT  “ YOU WILL NOT GET ADMISSION 
END IF


Note inner bracket is very important here. If you do not put the extra set of bracket for ( PM > 70 AND TM>60) , it mean QBASIC might understand the logic in some different way. For the condition, IF ( ATT > 80 OR PM > 70 AND TM>60 ) , QBASIC might think the logic is like , attendance should be greater than 80% or practical marks should be greater than 70%, but surely students have to get 60% in theory paper. It is a wrong interpretation by the QBASIC code then. So to confirm the implementation of correct logic, better to use brackets.





Now take another example from the last chapter of nested-if.

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 “

Our solution was like below

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
            END IF
END IF

                                                             

                                               

                                    
We can rewrite the code again using AND , OR

INPUT “ ENTER PERCENTAGE OF ATTENDANCE  “ ; ATT
IF ( ATT > 100 ) THEN 
PRINT  “INVALID ATTENDANCE “
IF ( ATT >= 91 AND ATT <=100 ) THEN 
PRINT  “ SUPERB !! 
IF ( ATT >= 75 AND ATT <=90 ) THEN  
            PRINT  “ GOOD TRY TO BE MORE FREQUENT “ 
IF ( ATT >= 50 AND ATT <= 74 ) THEN 
            PRINT  “ CONSIDERING LAST TIME “
IF ( ATT >= 25 AND ATT <= 49 ) THEN
            PRINT  “ GUARDIAN CALL “
ELSE 
            PRINT  “NO NEED TO GIVE EXAM “
END IF
END

Do you think that the above code is correctly converted? If the input is given as 95, means att=95, then what will be the output?

Output of the above program would be

Superb
No need to give exam

Strange output. Right? It happened because of the independent IF blocks. In case of att =95, it will go through all the IF blocks separately and will throw the following status

So let us see what happened exactly in case of att=95

IF (ATT >100 )                                 àreturns FALSE, No ELSE  block, nothing will be printed 
IF (ATT >= 91 AND ATT <=100)  à returns TRUE and will print “Superb” 
IF (ATT >= 75 AND ATT <=90 )   à returns FALSE, No ELSE  block, nothing will be printed 
IF (ATT >= 50 AND ATT <= 74à returns FALSE, No ELSE  block, nothing will be printed
IF (ATT >= 25 AND ATT <= 49)   à returns FALSE, will go to the ELSE  block and will print “no Need to give the exam”.

It means the last ELSE  block does not belong to other IF blocks.  It is only linked with the last independent IF block.

Ideally , the flow should be stopped after the first match itself. So it is always a best practice to put everything inside the Nested-IF to avoid this kind of unwanted errors. We will be ending this chapter with the correct code of Composite IF.


INPUT “ ENTER PERCENTAGE OF ATTENDANCE  “ ; ATT
IF ( ATT > 100 ) THEN 
PRINT  “ INVALID ATTENDANCE 
ELSE
IF ( ATT >= 91 AND ATT <=100 ) THEN
PRINT  “ SUPERB !! 
ELSE
IF ( ATT >= 75 AND ATT <=90 ) THEN
                                    PRINT  “ GOOD TRY TO BE MORE FREQUENT “ 
ELSE
IF ( ATT >= 50 AND ATT <= 74 ) THEN

                                                PRINT  “ CONSIDERING LAST TIME “
ELSE
IF ( ATT >= 25 AND ATT <= 49 ) THEN
                                                            PRINT  “ GUARDIAN CALL “
ELSE 
PRINT  “NO NEED TO GIVE EXAM “
                                                END IF
                                    END IF
                        END IF
            END IF
END IF

END



                                            

                             

              














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 ..