Page 80 - iPro_trackGPT_V5_Class8
P. 80

The syntax of the if…else statement is as follows:

                  if (< conditional expression >)                          Start
                  {
                         [statements]

                  }                                                     Conditional     False
                                                                         Expression
                  else
                                                                            of if
                  {
                         [statements]                                         True        Conditional    False
                                                                                           Expression
                  }
                                                                         Body of if        of else if
                  For example,

                  public class IfElseStatement                                                  True
                  {                                                                      Body of else if  Body of else
                        public static void main(int num)
                                                                           Stop
                        {
                             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.



                    78    TrackGPT iPRO (V5.0)-VIII
   75   76   77   78   79   80   81   82   83   84   85