Page 89 - Trackpad_ipro 4.1_Class8
P. 89

Logical Errors

                 A logical error is a type of error due to which a program compiles and executes successfully
                 but gives an unexpected or incorrect result. It is very difficult to find this type of error in the
                 program. The compiler will not be able to find logical errors. We need to read our programs
                 deeply to find logical errors. Logical errors are also called semantic errors. For example, if a
                 programmer accidentally adds two numbers instead of dividing them, then they will not get the

                 expected output.
                 public class LogicalError{

                        public static void main(String args[])
                        {

                               int a = 100;

                               int b = 200;
                               int c = a + b;

                               System.out.println("The result of division is: "+c);

                        }
                 }

                 When we compile and execute the preceding program, it does not show any error message.
                 However, it gives an incorrect output because we have used the plus (+) sign instead of the
                 divide (/) sign.

                     WRITING SOME MORE PROGRAMS

                 Till now, we have learned conditional, loop, and jump statements of Java. We have also created
                 related programs in Java. Let us now create some additional programs.
                 Program 1: Write a Java program to print all the even numbers between 1 and N by using the
                 'while' loop. The value of N should be taken from the user as input.

                 public class Program1
                 {
                     public static void main(int N)
                     {
                         int i = 1;
                          System.out.println("Even numbers
                            between 1 to "+ N+ " are:");
                         while (i <= N)
                         {
                             if(i%2==0)
                             {
                             System.out.println(i);
                             }
                             i++;


                                                             Conditional, Looping and Jumping Statements in Java  87
   84   85   86   87   88   89   90   91   92   93   94