Page 203 - CA_Blue( J )_Class9
P. 203

3. Using do-while loop:
                                                class null_dowhile
                                                 {    public static void main()
                                                    {         int i=1;
                                                        do
                                                        {
                                                 } while(i>=1);
                                                        }
                                                }
                 There will be an infinite delay in 2 and 3 as the loop runs infinitely.

                 9.3.3 Extra Expression/Statements
                 In a "for” loop we can have more expressions.
                 Syntax:
                 for (initialization1, initialization2 ….; testing condition; increment1/decrement1,
                 increment2/decrement2 )
                    {
                            //Code to be executed
                    }

                                                For Example:
                                                int i,j;
                                                    for(i=1,j=10;i<=5;i++,j--)
                                                    {
                                                        System.out.print ((i+j)+" ");
                                                    }
                                                You will get the following output:
                                                11 11 11 11 11

                 Explanation : In the above code, the program will print 11 five times because in every iteration the value of i
                 increases by 1 and the value of j decreases by 1 and the added value is printed. This will continue as long as the
                 value of i is less than or equal to 5.


                 9.3.4 Finite Loop
                 When the loop block executes for a finite number of steps               FINITE LOOP
                 then the loop is known as a Finite Loop. There are two types
                 of finite loops:

                 Continuous Loop: Here the increment or decrement changes   Continuous Loop           Step Loop
                    by 1 only for each iteration.

                 For Example:

                                             Print all the natural numbers up to 10.
                                                 for (i=1; i<=10; i++)
                                                 {
                                                    System.out.println(i);
                                                 }






                                                                                       Iterative Constructs in Java  201
   198   199   200   201   202   203   204   205   206   207   208