Page 163 - CA_Blue( J )_Class9
P. 163

Syntax:

                    if (condition1) {
                        // Code to execute if condition1 is true
                    }
                    if (condition2) {
                        // Code to execute if condition2 is true
                    }
                    if (condition3) {
                        // Code to execute if condition3 is true
                    }

                                                   Start



                                                Input values
                                                  a and b




                                                                  true     Print Both a and b
                                                  if (a==b)                     equal


                                                 false



                                                                   true     Print a and b not
                                                  if (a!=b)                     equal

                                                false


                                                                   true    Print a is greater
                                                   if (a>b)
                                                                                than b

                                                false

                                                   Stop




                       Note: 'In Java, when there is only one statement in an if or else block, the curly braces {} are optional.
                       You can omit the braces and just write the statement directly.



                 Example:

                        int n1=10, n2=22, n3=15;
                        if(n1>=15)
                            System.out.println(n1 + "is greater than or equal to 15");
                        if(n2>=15)
                            System.out.println(n2 + "is greater than or equal to 15");



                                                                                     Conditional Constructs in Java  161
   158   159   160   161   162   163   164   165   166   167   168