Page 198 - ComputerScience_Class_11
P. 198

28                      break;
                29                  case 2:
                30                      System.out.println("The choice is 2");

                31                      break;
                32                  case 3:
                33                      System.out.println("The choice is 3");

                34                      break;
                35                  default:
                36                      System.out.println("Wrong choice");

                37                      break;
                38              }
                39          }

                40      }
              The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java
                 Options

                Enter number ch1 and character ch2
                1
                B
                The choice is B



                     BlueJ: Terminal Window - Java
                 Options

                Enter number ch1 and character ch2
                2
                B
                Choice is 2


              Difference between if statement and switch statement:
                                       if statement                               switch statement
                      The flow of control is bidirectional.        The flow of control is multidirectional, i.e., depending
                                                                   on the user’s choice.
                      All kind  of relational  operators are  used in the  test  Only int, char and string data type value is used in the
                      condition.                                   expression.
                      Any type of data type can be used for the variables  Only int, char and string data types are used for the
                      used in the test condition.                  variables used in the test condition.


              8.4.3 Iterative Statements
              Sometimes, we may require to repeat certain lines of the program according to the logic or need of the program.
              A programming language like Java allows the statements to repeat a specified number of times. This repetitive flow of
              control in a program is called a loop and the repetitive statements are called iterative statements. The loop is used to
              execute a certain number of lines again and again to get the desired result.



                  196  Touchpad Computer Science (Ver. 3.0)-XI
   193   194   195   196   197   198   199   200   201   202   203