Page 78 - iprime_V2.2_class8
P. 78

The syntax of the 'if...else' statement is as follows:

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

                  }
                                                                                 Test        False
                  else                                                                                 Body of else
                                                                              Expression
                  {
                         [statements]
                                                                                    True
                  }
                  For example:                                                 Body of if
                  public class IfElseStatement

                  {
                                                                                 Stop
                        public static void main(int num)
                        {                                                         Flowchart 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 values of 22 and 7, respectively.

                  When you enter 22, the condition (22 % 2 == 0) returns
                  true. So, the output appears as shown.

                                                                                         Output When Input Is 22
                  When you enter the value 7, then the output appears
                  as shown.

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

                  76     iPRIME (Ver. 2.2)–VIII
   73   74   75   76   77   78   79   80   81   82   83