Page 76 - iPlus_Ver_2.0_class_8
P. 76

The syntax of the if…else statement is as follows:
                                                                                 Start
                  if (< conditional expression >)
                  {
                         [statements]

                  }
                                                                              Conditional     False
                  else                                                        Expression                Body of else

                  {
                         [statements]
                                                                                    True
                  }

                  For example,                                                 Body of if
                  public class IfElseStatement

                  {
                                                                                 Stop
                        public static void main(int num)
                        {
                                                                               Flow chart of if ....else statement
                             System.out.println("Entered number is: " + num);
                             if (num % 2 == 0)

                             {
                             System.out.println("The number is even");
                             }

                             else
                             {

                             System.out.println("The number is odd");
                             }
                             System.out.println("Statement outside the if...else statement");

                        }
                  }

                  As you did in the previous example, run the program
                  twice for the values of 22 and 7, respectively. When
                  you enter 22, the condition (22 % 2 == 0) returns true.
                  So, the output will appear as shown:




                                                              When you enter the value 7, then the output will
                                                              appear as shown:

                                                              Because the condition (7% 2 == 0) in the if statement
                                                              returns false, control is transferred to the statements
                                                              in the else block.



                    74
                         iPlus (Ver. 2.0)-VIII
   71   72   73   74   75   76   77   78   79   80   81