Page 121 - CA_Blue( J )_Class10
P. 121

16                  System.out.println("The profit : " + p);
                  17              }

                  18              else
                  19              {

                  20                  l = cp - sp;
                  21                  System.out.println("The item was sold in loss. ");

                  22                  System.out.println("The Loss : " + l);
                  23              }

                  24              System.out.println("----------------------------------");
                  25          }

                  26      }
                 You will get the following output:























                       Note: A group of statements within curly brackets together is called compound statement.



                 7.2.3 The if and Only if Statement
                 Some situation requires checking all the conditions one after another. Here, if the 1st condition is satisfied, then
                 statement 1 will execute. If the 2nd condition is satisfied, then statement 2 will execute and so on, thus executing all
                 the conditions. Syntax of the if and only if statement is:

                    if(condition 1)
                        statement 1;
                    if(condition 2)
                        statement 2;
                    if(condition 3)
                        statement 3;
                 Example of the if and only if statement is:

                    int a=4, b=2,c=0;
                    if(a==0)
                        System.out.println(a + " is equal to 0");


                                                                                                                       119
                                                                                           Conditional Constructs in Java  119
   116   117   118   119   120   121   122   123   124   125   126