Page 161 - CA_Blue( J )_Class9
P. 161

5        {
                   6              Scanner sc= new Scanner(System.in);

                   7              int n;
                   8                System.out.print("Enter a number from user : ");

                   9              n = sc.nextInt();
                  10              if(n>0)
                  11              {

                  12                  System.out.println(n+ " is a positive number ");

                  13              }
                  14              if(n%2==0)
                  15              {

                  16                  System.out.println(n+ " is a even number ");
                  17              }

                  18          }
                  19      }

                 You will get the following output:

















                 if-else statement                                            Start
                 Some programs need to choose any one of the two
                 actions depending on the requirement of the program.
                 In Java, an if-else statement executes the if block if the   Input values a & b
                 condition  is  true.  Otherwise,  the  else  block  will  be
                 executed.
                 Syntax:

                 if (condition)                                             if (a==b)        true       Print "Equal"
                 {
                 // Code to execute if the condition is true
                 }                                                              false
                 else
                 {                                                      Print "No Equal"
                 // Code to execute if the condition is false
                    }
                                                                              Stop



                                                                                     Conditional Constructs in Java  159
   156   157   158   159   160   161   162   163   164   165   166