Page 13 - Touchcode_C8_Flipbook
P. 13

Experiential Learning
                                                      Coding Task          01


                                                         To create a triangle

                  Triangle has three sides, and to draw a triangle the sum of two sides must be greater than
                  the remaining side.

                  Suppose you want to draw a triangle with sides A, B, C. Unless all sides are compared, you
                  cannot say values for A, B and C are valid or not. Therefore, you need to combine all three
                  logical expressions to say whether they can make a triangle or not.


                                                                              A + B > C



                                              A                B              B + C > A


                                                                              C + A > B
                                                      C



                  If we write the above problem in python, the code will be:

                  A=12
                  B=14
                  C=18
                  if (A+B>C) and (B+C>A) and (C+A>B):

                      print(“Yes! Triangle can be drawn”)





                      Well done, you have learned to create a triangle!




                 PRECEDENCE OF LOGICAL OPERATORS
                 Logical operators also have precedence which determines how the operations are grouped

                 in the absence of parentheses.

                 Parentheses are used to group the          A + B > C              B + C > A             C + A > B
                 operands with their correct operator.
                 In an expression, the operator with
                 the highest precedence is grouped          A + B > C     and      B + C > A     and     C + A > B
                 with its operands first, then the next

                 highest operator will be grouped with its operands, and so on.

                                                                                        Conditionals in Details  11
   8   9   10   11   12   13   14   15   16   17   18