Page 245 - IT-802_class_12
P. 245

Common Coding Errors: Loops
            1.  Infinite Loops

               This type of error occurs when the loop runs forever, the loop never exits. This happens when the test condition is
              always true.
              For example,

              int number = 1;
              while (number <= 5)
               {
              System.out.print(“Square of “ + number);
              System.out.println(“ = “ + number*number);

               }
              This loop will run forever since number never changes – it remains at 1.

            2.  Syntax Errors
              Ð ÐDo not forget the semi colon at the end of the test condition in a do-while loop. Otherwise, you will get a
                 compiler error.

              Ð ÐDo not place a semi colon at the end of the test condition in a while loop.
                For example,

                int x = 1;
                while ( x <=5 );
                x = x +1;

              Ð ÐThis loop is an infinite loop - The semicolon after the while causes the statement to be repeated as the null
                 statement (which does nothing). If the semi colon at the end is be removed the loop will work as expected.

              3.5 oBJEct-orIEntEd ProgrammIng (ooP)

            A programming model that depends on the theory of classes and objects, and gives importance on data rather than
            functions is known as Object-oriented programming (OPP). It splits the programming code into number of entities,
            known as objects. It increases the ability to deal with complex software problems— particularly for developing and
            maintaining large real-life applications. Using this technique, writing source code that helps to create different objects
            from a common structure becomes easy. This common structure is usually called a blueprint or class and the objects that
            are created called instances.
              3.6 aSSErtIonS

            A useful tool for effectively identifying/detecting and repairing logical flaws in a program is an assertion. When writing
            Java program using assert statements to debug your code is a recommended programming practice. An assert statement
            specifies a condition that must be true at a specific moment during the program’s execution.
            The first statement evaluates expression and throws an AssertionError if expression is false. The second statement
            evaluates expression1 and throws an Assertion Error with expression2 as the error message if expression1is false.
            The program fragment below demonstrates usage of the assert statement.

            assert age >= 18:”Age not Valid”;
            When this statement is executed, we assert that the value of the variable age should be >= 18. If it is not, an Assertion
            Error is thrown and the error message “Age not Valid” is returned.





                                                                               Fundamentals of Java Programming  243
   240   241   242   243   244   245   246   247   248   249   250