Page 206 - ComputerScience_Class_11
P. 206

•  Nested do-while loop: The do-while loop inside another do-while loop is called a nested do-while loop.
                For example:

                              Write a Java program using nested do-while loops to print the following pattern:
                Program 9
                              3
                              2 2
                              1 1 1
                1       import java.util.Scanner;

                2       public class NestedDoWhileLoop {
                3           public static void main(String[] args) {
                4               int i;

                5               int j;
                6               i=3;
                7               do

                8               {
                9                   j=3;
                10                  do

                11                  {
                12                      System.out.print(i+" ");
                13                      j--;
                14                  } while(j>=i);

                15                  System.out.println();
                16                  i--;
                17              } while(i>=1);

                18          }
                19      }

              The output of the preceding program is as follows:
                     BlueJ: Terminal Window - Java

                 Options

                3
                2 2
                1 1 1



              Jump Statement
              Some statements transfer the program control from one part of the program to another part depending on a certain
              condition. Such statements are called jump statements. They are also a type of control statements.
              In Java, there are three jump statements which are as follows:
              •  break: In Java, the break statement is used to exit from the loop or switch statement. The program pointer goes
                 to the line that follows the loop or switch statement. The statements after the break statement inside the loop or
                 switch will not be executed. It is used for an unusual termination of the loop. The break statement can be used with
                 all the different types of loops.



                  204  Touchpad Computer Science (Ver. 3.0)-XI
   201   202   203   204   205   206   207   208   209   210   211