Page 82 - Viva ICSE Computer Studies 8 : E-book
P. 82

If Else Statement
                    In the If Else statement structure, if the condition given is ‘True’, then the statement specifi ed
                    within If block is executed. If the condition is ‘False’, the statement following Else will be
                    executed. Let’s understand If Else statement with the syntax and an example.

                    Syntax:

                      If (Test condition)
                      {
                                                                             False                      True
                          ...................................................           Condition

                      Statements x
                      }                                            Statements y                            Statements x
                     Else

                      {
                          ...................................................
                                                                                      Rest of the code
                      Statements y
                      }

                    Example:  Write a program to fi nd the smallest of two numbers.































                    If Else If Ladder

                    If Else If ladder is used to check multiple conditions in a program. This can be executed by
                    including multiple Else If statements. If the condition written inside the If block is ‘True’, the
                    statement associated with that gets executed, and the rest of the ladder is skipped.  The
                    statement inside an Else block is executed only when all the conditions that come before it in
                    the ladder are ‘False’. But at a time only one condition can be executed.

                    Let’s understand If Else If ladder with the syntax and an example.



                     70
   77   78   79   80   81   82   83   84   85   86   87