Page 180 - Computer science 868 Class 12
P. 180

For example:
              Input a number and print whether the number is greater than 45 or not.

                  class ifelse
                  {
                      public static void main(double num)
                      {
                          if(num>45)
                          {
                              System.out.println(num + " is greater than 45");
                          }
                          else
                          {
                              System.out.println(num + " is not greater than 45");
                          }
                      }
                  }
              Output:

              80.0 is greater than 45
              Let us take some more examples:

              a.  if(max<100)
                  System.out.println(max + " is less than 100");
                else
                  System.out.println(max + " is greater than or equal to 100");

              b.  if(sal>=50000)
                  {
                    inctax = 4.0/100.0 * sal;
                    net_sal = sal – inctax;
                  }
                else
                  {
                    inctax = 2.0/100.0 * sal;
                    net_sal = sal – inctax;
                  }
                System.out.println("Salary : " +sal);
                System.out.println("Tax : " +inctax);
                System.out.println("Net salary : "+net_sal);
              Here, if the salary is more than 49999, the tax is 4% of sal else 2%.


              if and only if Statement
              The if and only if statement is required when all the conditions need to be checked and whichever conditions are
              satisfied, the statements under those conditions will be executed.
              Syntax:

                  if(condition 1)
                  {
                      Statement 1;
                  }


                178178  Touchpad Computer Science-XII
   175   176   177   178   179   180   181   182   183   184   185