Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 102 - 2617_JSSPS_C-7
P. 102

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

                  if (< conditional expression >)

                  {

                      [statements]
                  }

                  else

                  {

                      [statements]
                  }

                    Program 6:  Write a program to check whether the given number is even or odd.

                  1   public class IfElseStatement
                  2   {

                  3       public static void main(int num)
                  4       {
                  5          System.out.println("Entered number is: " + num);

                  6          if (num % 2 == 0)
                  7          {
                  8          System.out.println("The number is even");

                  9          }

                  10         else
                  11         {
                  12         System.out.println("The number is odd");

                  13         }
                  14         System.out.println("Statement outside the if...else statement");

                  15      }
                  16  }
                  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:

                            Premium Edition-VII
                    100

| 97 98 99 100 101 102 103 104 105 106 107 |