if-else in Java programming Hands on Coding

 If else statement in Java;


In this section, let's discuss about another conditional statement I.e if-else.


Syntax:

  • This is used to test the conditions.
  • If the given condition is true then body of if will execute otherwise the body of else block will be executed.


Example 1:

 class Easy   
 {   
   public static void main(String[] args)   
   {    
    int a=10,b=20;   
    if(a>b)      //a greater than b(false)   
    {   
      System.out.println("a is greater");   
    }   
    else   
     {   
      System.out.println("b is greater");   
     }   
    }   
   }   
 / ### Output ### b is greater /    


Example 2: Program to check whether ths given number is even or odd.

 import java.util.Scanner;   
 Class Easy   
 {   
   public static void main(String[] args)   
   {   
     Scanner in=new Scanner(System.in);   
     int no;   
     System.out.println("Enter any number");   
     n=in.nextInt();  //number exactly devided by 2 is called even   
     if(n%2==0)   
     System.out.println("number is even");   
     else   
     System.out.println("number is odd");   
   }   
 }   
 / ### Output ###   
 Enter any number 5 number is odd /   

For Videos Join Our Youtube Channel: Join Now

Thank you!