Page 147 - computer science (868) class 11
P. 147

if-else Statement
                 Using an if-else statement, either of the two actions will be executed. If the condition is satisfied, then the first part will
                 be executed, else the second part will be executed.
                 Syntax:

                    if(Condition)                                                   Yes                      No
                    {                                                                         Condition
                                                                                                 ?
                        Statement 1;
                    }                                                        Statement 1                    Statement 2
                    else
                    {
                        Statement 2;
                    }
                                                                                      Flowchart of if-else statement
                 For example,
                 1.  To check whether the two numbers are equal or not.
                    int a=6, b=7;
                    if(a==b)
                        System.out.println("Numbers are equal");
                    else
                        System.out.println("Numbers are not equal");
                 2.  To check whether the basic salary is more than 1999, then the tax is 5% of the basic salary else 2.5%.
                    if(basic_sal>=2000)
                        tax= 5.0/100.0*basic_sal;
                    else
                        tax= 2.5/100.0*basic_sal;

                        System.out.println("Tax : "+tax);
                 Here, if the basic salary is more than 1999, the tax is 5% of the basic salary else 2.5%.

                 if and only if Statement                                                                Yes
                 Sometimes, there is a situation when all the conditions need to be checked   Condition 1?
                 and whatever conditions are satisfied, the statements under them are                       Statement 1
                 executed. It may happen that all the conditions are satisfied.        No
                 Syntax:

                    if(Condition1)                                                                       Yes
                                                                                       Condition 2?
                    {
                        Statement 1;
                                                                                                            Statement 2
                    }                                                                  No
                    if(Condition2)
                    {
                                                                                                         Yes
                        Statement 2;
                                                                                       Condition 3?
                    }
                    if(Condition3)
                                                                                                            Statement 3
                    {                                                                  No
                        Statement 3;
                    }
                                                                                        Flowchart of if and only if statement


                                                                                                                       145
                                                                                                 Statements and Scope  145
   142   143   144   145   146   147   148   149   150   151   152