Skip to main content
Java if else statement

    If else statement is a conditional statement that executes a block of code once the condition is true otherwise 
executes else block if the condition becomes false.

   Syntax :
            if (condition) {
                  //  code to execute
             } else {
                     //  code to execute
                  }

    Example :

        public class conditionalProg { 
                 public static void     main(String args[]) { 

                  /* declaration and
                    initialization of 
                     variables */

                  int a = 3, b = 1;
                   
                          //Condition
                           if (a < b) {       

     //this block will execute if the    condition
       is true
 System.out.print("a is less than b");

                            } else

     //this block will execute if the    condition
       is false
System.out.print("a is greater than b");
                              
                               }
                   }
   
           }

Comments