Page 170 - Cs_withBlue_J_C11_Flipbook
P. 170

Let us see in detail.

              if Statement
              This statement checks a particular condition. If the condition is satisfied, then the statement or a group of statements
              inside the “if” block will be executed.
              Syntax:

                  if(Condition)
                  {
                      Statement 1;
                      Statement 2;
                      .
                      .
                  }

                      Note:  If there is single statement after the condition, then use of curly brackets is optional.



              Flowchart:


                                                        if(condition)?


                                                                              Statement






              For example,

              1.  if(a%5==0)
                      System.out.println(a+ " is divisible by 5");
                Here, the control statement checks whether the number stored in the variable a is divisible by 5. If it is divisible, the
                 following message will be printed, otherwise, the message will be ignored.

              2.  if(a%5==0 && a%2==1)
                {
                    System.out.println("The number is divisible by 5");
                    System.out.println("It is an odd number");
                }
                Here, if a%5 returns 0 and a is an odd number, the two messages will be printed.

              3.  if(c==0)
                {
                    c=a++ + ++b;
                    System.out.println("c= "+ c);
                }
                System.out.println("Outside if block");
                Here, if c==0, then the value of c will be changed to 15 and displayed. However, if c==0 is false, the message
                 “Outside if block” will be displayed.






                168168  Touchpad Computer Science-XI
   165   166   167   168   169   170   171   172   173   174   175