Translate

If else statement in Java.

If else statement in Java

 
  • It is used to test the condition.
  • If the condition is true body of if will execute otherwise body of else execute.
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
*/
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");
  no=in.nextInt();
  //number exactly devided by 2 is called even
  if(no%2==0)
   System.out.println("number is even");    
  else
   System.out.println("number is odd");        
 }
}
/*
### Output ###
Enter any number
5
number is odd
*/
Next Post Previous Post
No Comment
Add Comment
comment url