skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, January 7, 2012

Comparison of Two Variable using If | JAVA Program

Posted by Admin at 2:05 AM – 0 comments
 
Here is a Java Program to show Comparison Operation on the two variable using If Control Structure

class IfComparison {
 public static void main(String args[]) {
  int x, y;

  x = 10;
  y = 20;

  if(x < y) System.out.println("x is less than y");
  
  x = x * 2;// x= 20
  if(x == y) System.out.println("x now equal to y"); 
  
  x = x * 2;
  if(x > y) System.out.println("x now greater than y");
  
  // this won't display anything
  if(x == y) System.out.println("you won't see this");
 }
}
/***************************
 * x is less than y
 * x now equal to y
 * x now greater than y
 *
 ****************************/


Explanation of Above Java Program:
In above Program we have two variable x and y with value of 10 and 20 respectively .

If Syntax
if( Condition){
    //code goes here that would execute if condition is true 
}

  1. first we compared if X less than y
  2. We multiply X with 2
  3. we check to see if they are equal or not
  4. then again multiply with two
  5. Check to see if x is greater than y
  6. Finally we check if two variable equal or not.
Labels: Control Structure , If Condition

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger