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

Popular posts from this blog

Array in java Array is a variable that can hold more than one value at a time. Declaration :    int arr[] = {elements}    or    int [] arr = {elements} Intialization :    int arr[] = {2, 4, 6, 7,}     Example :    public class arrLesson {                public static void main(String           args[]) {                // declares an array                 int arr[] = { 2, 3, 2, 9, 3 };                                 for ( int i=0; i < arr. length ;       i++)      {                      /* outputs each element                     ...

Method Overloading in Java Best Example For Biggeners and Students.

class  plus{   static   int  add( int  a, int  b) {     return  a+b; }      static   int  add( int  a, int  b, int  c)     {       return  a+b+c;    }   }   class  Overloading1 {   public   static   void  main(String[] args)    {         //access the add method with 2 param from class plus,      //pass 2 arguments and print the return value      System.out.println (plus.add( 10 , 10 ));         //access the add method with 3 param from class plus,        //pass  3 arguments and print the return value       System.out.println (plus.add( 10 , 10 , 10 ));       } }   It will produce the following results: 20 30