• Servlet
  • PHP
  • Core Java
  • SQL Tutorial
  • JSP
  • Spring
  • JavaScript
  • Struts
  • RubyonRails

  • <

    Java If Else Tutorial

    Java if statement is used to check boolean conditions True and False .If is executed when condition is true and when condition is false else is executed .


    Java If Else Syntax:


    
    if(test_expression)
    {
       //execute your code
    }
    else if(test_expression n)
    {
       //execute your code
    }
    else
    {
       //execute your code
    }
    

    Java else if statement example


    public class Example {
    
        public static void main(String args[]) {
            int x = 2,y = 2;
    
            if (y > x) {
                System.out.println("b is greater");
            }
            else if(x > y){
                System.out.println("a is greater");
            }       
            else {
                System.out.println("Both are equal");
            }
        }
    }
    

    OUTPUT:
    Both are equal
    

    Java if else statement


    syntax:
    
    if (expression) {
       // codes
    }
    else {
      // some other code
    }
    

    Java Nested if else


    class Number {
        public static void main(String[] args) {
    
            Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber;
    
            if (n1 >= n2) {
                if (n1 >= n3) {
                    largestNumber = n1;
                } else {
                    largestNumber = n3;
                }
            } else {
                if (n2 >= n3) {
                    largestNumber = n2;
                } else {
                    largestNumber = n3;
                }
            }
    
            System.out.println("Largest number is " + largestNumber);
        }
    }
    

    OUTPUT:
    Largest number is 4.5
    













    © copyright 2017-2021 Completedone pvt ltd.