If else statement in Java

Hello friends how are you, today in this blog i will teach you what is if else statement, syntax of if else , how if else statement works and many programs using if else in a very simple way. 

Let's start

 


  • If else is a part of conditional statement.
  • It is used to test the condition.
  • If the condition is true body of if will execute otherwise body of else execute.

Here in this example value of a is 10 and value of b is 20 , condition is a>b means it is false because 10 is not greater than 20 so because of false condition else part will execute and output will be b is greater .

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
*/

Here in this example we are going to check given number is even or odd . number which is divisible by 2 and generates a remainder of 0 is called even number.

So here we will take a single variable for number and after taking user input we will apply condition on it. After dividing the number by 2 if the remainder is 0 then we will print Even otherwise we will print 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");
  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
*/

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. 

Try to learn something new and teach something new to other. 

Thanks.😊

Post a Comment

0 Comments