Page 155 - computer science (868) class 11
P. 155

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 kinds 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.

                 7.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.
                 Each looping structure contains four parts:

                 •  Starting value/initialisation
                 •  Test condition
                 •  Increment or decrement (also known as step value)
                 •  Body of the loop
                 Let us take an example:

                    for(i=1; i<=10; i=i+2)
                    {
                        System.out.println(i);
                    }
                 Here,
                 •  Starting value → i=1;

                 •  Test condition → i<=10;
                 •  Increment → i=i+2
                 •  Body of the loop → System.out.println(i);
                 There are two types of loops based on the nature of repetition which are as follows:
                 1.   Fixed Iteration Loop: Fixed type of iterative loop repeats the process for a defined number of times. For example,
                   for loop
                 2.  Unfixed Iteration Loop: Unfixed type of iterative loop repeats the process till a given condition is true. For example,
                   • while loop
                   • do-while loop

                 There are two types of loops based on the condition which are as follows:
                 1.  Entry Controlled Loop: If the test condition is checked before executing the body of the loop, then it is called an
                   entry controlled loop. For example,
                    • for loop
                    • while loop
                 2.  Exit Controlled Loop: If the test condition is checked after executing the body of the loop, then it is called an exit
                   controlled loop. For example,
                   • do-while loop



                                                                                                                       153
                                                                                                 Statements and Scope  153
   150   151   152   153   154   155   156   157   158   159   160