Page 89 - 2502_Pakistan-kifayat_C-7
P. 89

Step 3:   Calculate area = 3.14 * radius * radius.

                 Step 4:  Display area.
                 Step 5:  Stop

                 More Than One Way to Solve a Problem

                 Sometimes, a single problem can have more than one correct algorithm to solve it. This is because
                 there can be different ways of thinking or solving a task logically.

                 Example 3: To check if a number is even
                 Method 1: Check if number is divisible by 2


                 if number % 2 = 0 then
                   Print "Even"

                 Method 2: Check if the last digit is 0, 2, 4, 6, or 8

                 if last digit in (0, 2, 4, 6, 8) then

                   Print "Even"
                 Both methods are correct and give the same result.


                      USING IF AND IF-THEN-ELSE CONDITIONS

                 While writing a program, we need to make certain decisions based on the logic of the program. Such
                 situations are called conditions. Conditional statements are used in a program to instruct the computer
                 to make a decision. The result of a condition will always be either True or False. The different conditional
                 statements used in a program are:

                 The if Statement

                 The if statement is the simplest conditional statement. Here, a statement or a collection of statements
                 within the if block is executed only if a certain condition or expression evaluates to True. If the condition
                 evaluates to False, then the control of execution is passed to the next statement after the if block.


                                                              Start



                                                                              False
                                                           if condition

                                                                 True

                                                         Statements in if
                                                          block execute



                                                              Stop

                 Syntax:

                 if (Test Expression)


                                                                                                 #Logical Reasoning  87
   84   85   86   87   88   89   90   91   92   93   94