Page 87 - 2502_Pakistan-kifayat_C-6
P. 87

The algorithm takes two numbers as input, adds them together, and displays the result. This is a simple
            example where each step is executed sequentially—getting the numbers, performing the addition, and
            showing the result.

            Example 3: Calculate the Area of a Rectangle

            Algorithm:
            Step 1:   Start.                                                           length

            Step 2:   Input the length of the rectangle (Length).

            Step 3:   Input the width of the rectangle (Width).
                                                                                                              width
            Step 4:   Calculate the area (Area = Length × Width).

            Step 5:   Print "Area".

            Step 6:   Stop.


            This algorithm calculates the area of a rectangle. First, it takes the length and width as input. Then, it
            multiplies the two values to find the area and displays the result.


                 CONDITIONAL (SELECTION) STATEMENT


            Conditional  or selection statements are used  in algorithms to make decisions  based on certain
            conditions. They allow the algorithm to choose different paths of execution depending on whether a
            given condition is TRUE or FALSE. These are fundamental in decision-making processes within programs
            and are widely used in programming languages.

            How Conditional Statements Work in Algorithms?

            In an algorithm, a conditional statement typically takes the following structure:

            1.  Evaluate the condition: Check if a specific condition or set of conditions is true or false.
            2.  Select a path: Depending on the evaluation result, either:

                  Execute a specific set of instructions if the condition is true.

                  Skip or execute a different set of instructions if the condition is false.

            Examples of Conditional Statement Algorithms

            Let’s look at some examples to understand the concept of conditional statements.

            Example 4: Determine If a Number is Even or Odd
            Algorithm:

            Step 1:   Start.

            Step 2:   Get a number as input from the user.
            Step 3:   If the remainder when the number is divided by 2 is zero, go to Step 4; otherwise, go to Step 5.

            Step 4:   Print "The number is even".



                                                                                          #Algorithmic Thinking  85
   82   83   84   85   86   87   88   89   90   91   92