Page 179 - Computer science 868 Class 12
P. 179

Let us understand them one by one.
                 Simple ‘if’ Statement
                 The simple if statement consists of the if part only, which means it contains only the condition. Depending on a single
                 condition, the execution of the program is diverted as and when needed. The result of the if statement gives a boolean
                 value.

                 Syntax:
                    if(condition)
                        {
                            Statement 1;
                            Statement 2;
                            …..
                        }
                 For example:

                 Input the cost of a pen and print the discount of `10/-, to be availed only if the price is more than `50/-.
                    class onlyif
                    {
                        public static void main(double pr)
                        {
                            System.out.println("MRP of the pen : " +pr);
                            if(pr>50)
                            {
                               pr -= 10;
                            }
                            System.out.println("The cost of the pen is : " +pr);
                        }
                    }
                 Output:

                 MRP of the pen : 70.0
                 The cost of the pen is : 60.0
                 Let us take some more examples:

                 a.  if(max<100)

                   System.out.println(max + " is less than 100");
                 b.  if(a>99 && a<=999)
                   System.out.println(a + " contains 3 digits");
                 if-else Statement
                 This statement is used whenever we need either of the two actions to be executed. If the condition is satisfied, then
                 one set of statements under the if part will be executed, and if not satisfied, then the set of statements under the else
                 part will be executed.
                 Syntax:

                    if(condition)
                    {
                        Statement 1;
                    }
                    else
                    {
                        Statement 2;
                    }


                                                                                                                       177
                                                                                                 Statements and Scope  177
   174   175   176   177   178   179   180   181   182   183   184