Page 80 - 2502_Pakistan-kifayat_C-8
P. 80

What are Nested Conditions?

              A condition inside another condition is called nested conditions.
              For example,

              Start
              Input number age

              Input number score
              If age > 10 Then

                  If score > 50 Then
                      Print "Great Job!"
              End
              The code uses nested If conditions. It first checks if age is greater than 10. If this condition is true, it
              then checks if score is greater than 50. Only If both conditions are true, the message "Great Job!" is
              displayed. If either condition is false, nothing is shown.


                   USING IF WITH MULTIPLE CONDITIONS

              Sometimes in a program, we need to check more than one condition at the same time. In such cases,
              we use logical operators like AND or OR in the IF statement. This helps the computer make smarter and
              more accurate decisions.

              For example, if the weather is sunny and the temperature is 32:

              Start
              Input number temperature

              Set isSunny = TRUE
              If temperature > 30 AND isSunny = TRUE Then
                                                                                             Hintbot
                  Print "Go for a swim"

              End                                                              Use AND when both conditions must
              The pseudocode checks two conditions together:                  be true.
                                                                               Use OR when only one condition needs
                  Temperature must be more than 30.
                                                                              to be true.
                  isSunny should be true.

              If both conditions are true, it displays "Go for a swim". If either one is false, the message will not be shown.

              The OR logical operator is used when we want the condition to be true if at least one of the conditions
              is true. It is helpful in situations where only one condition needs to be met.
              For example, if the homework is complete:


              Start
              Set homeworkDone = TRUE
              Set isWeekend = FALSE
              If homeworkDone = TRUE OR isWeekend = TRUE Then




                  78  Premium Edition-VIII
   75   76   77   78   79   80   81   82   83   84   85