Page 240 - Ai_V1.0_Class9
P. 240

Some examples of syntax errors include:

                a + b = c
                SyntaxError: cannot assign to operator
                myname = Arshia
                SyntaxError: EOL while scanning string literal
                a = 5
                b = 10
                  c = a + b
                SyntaxError: unexpected indent
                print("Python"))
                SyntaxError: unmatched ')'

              Logical Error
              This kind of error is difficult to find since the program will run correctly but the desired output is not achieved.
              This happens if we give a wrong formula for the calculation to be done, write wrong logic for the problem to be
              solved through the code.

              For example,
              To calculate the average:
                p = marks1 + marks2 / 2 #instead of (marks1+marks2) / 2
              Preceding code produces a wrong output due to the logical error in formula. Let us take another example. To
              find the perimeter of rectangle,
                p = 2 * l + b            #instead of p = 2 * (l + b)

              Runtime Error
              Runtime errors occur during the execution of a program and can result from various issues such as incorrect
              input or output, undefined object errors, or division by zero errors. These errors can halt the program's execution
              unexpectedly, making them challenging to debug.

              Example of runtime error is:
                a = int(input("enter first number"))
                b = int(input("enter second number"))
                c = a / b
              If a = 5 and b = 0, then it will display:
                 ZeroDivisionError: division by zero


                       Control Statements

              As you know that a program is a set of instructions given to a computer to do a specific task. The order of
              execution of these instructions controls the flow of a program. There are three important ways to control the
              flow of a program which are interpreted by any programming language in a structured manner. These are:
              •  Sequential Statements            •  Selection Statements             •  Iterative Statements

              Let us learn about these in detail.

                       Sequential Statements


              A step-by-step process of executing code is called sequential flow of control. This is the default flow of execution
              of a program. It means that the computer will run the code one line at a time starting from line 1, followed by
              line 2 and so on until it reaches the last line of the program.


                    238     Artificial Intelligence Play (Ver 1.0)-IX
   235   236   237   238   239   240   241   242   243   244   245