Page 89 - CA_Blue( J )_Class10
P. 89

•  Using the assignment operator instead of the equality operator
                    if(a=b)
                    {
                        System.out.println("Both a  and b are equal");
                    }

                 Let us take an example:
                    void add()
                    {
                        int a, b, sum                                    //Line 1
                        a = 8;                                           //Line 2
                        sum = a + b;
                        System.out.println("Sum is: " sum);              //Line 3
                    }
                 In the above example, there are three syntax errors:
                   a.  In Line 1, there is no semicolon ( ; ) at the end of the statement.
                   b.  In Line 2, b is not initialized but it is used.
                   c.  In Line 3, + operator is missing to concatenate two strings.

                 If these errors are removed from the program, then the program will execute successfully.

                 5.6.2 Logical Errors
                 Sometimes, there may be some errors in the logic of the program due to which the program will not give appropriate
                 output. These type of errors is called logical errors. If these types of errors are there in a program, then the program
                 will be compiled and executed successfully but the desired output will not be achieved. Logical errors are also called
                 semantic errors. It is very difficult to find this type of error in a program because the compiler does not show any error
                 message. Some of the common causes of logical errors are:
                 •  Multiplying two numbers instead of adding them together
                    System.out.println("The sum of two numbers is: "+ (a*b));
                 •  Using incorrect expression or logic
                    float E, m, c;
                    m = 134.4;
                    c = 2.345;
                    E = m*c;
                 Let us take an example. The following method is written to print the natural numbers from 1 to 10:
                    void natural()
                    {
                        int i;
                        for(i=1;i<10;i++)
                        {
                            System.out.println(i);
                        }
                    }
                 Here, 1 to 9 naturals numbers will be printed instead of 10 due to the incorrect use of condition "<10". It should be
                 "<=10". But, this type of mistake cannot be handled by the compiler.

                 5.6.3 Runtime Errors
                 The errors that occurred during the execution of a successfully compiled program are called runtime errors. Generally,
                 these errors occurred whenever a user enters wrong data or some data that is not required. When a runtime error
                 occurs, a program may crash, in some cases, even the operating system may also crash or reboot. These errors are not
                 detected by the compiler. Some of the common causes of runtime errors are:




                                                                                                                       87
                                                                                                         Input in Java   87
   84   85   86   87   88   89   90   91   92   93   94