The Artima Developer Community
Sponsored Link

Java Buzz Forum
If Else Statment in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
If Else Statment in Java Posted: Jun 27, 2015 9:02 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: If Else Statment in Java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
If statement :
  • if statement in java is decision making statement in java.
  • if statement will have a condition and if that condition is true then the corresponding block will be executed.
  • if(condition){ //  }
If condition syntax:

  1. if(condition){
  2. //  statements
  3. }
 Sample program on if statement:


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifExample { 
  5.  
  6. public static void main(String[] args) {
  7.  
  8. if(true){
  9.  
  10.  System.out.println("if condition executed");
  11.  
  12. }
  13.  
  14. }

Output:
  1. if condition executed

Java program to compare two numbers in java


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class simpleIfExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.   int x= 37;
  9.   int y= 37;
  10.                
  11. if(x==y)
  12.     System.out.println(x+ " is equal to " + y);
  13. }
  14.  
  15. if(x>y){
  16.     System.out.println(x+ " is greater than " + y);
  17. }
  18.  
  19. if(x<y){
  20.     System.out.println(x+ " is less than " + y);
  21. }
  22. }

Output:
  1. 37 is equal to 37

 Else Statement:

  •  if(condition){ //} else { // }

Java program to compare two numbers in java using if else


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.   int x= 37;
  9.   int y= 37;
  10.                
  11. if(x==y){
  12.     System.out.println(x+ " is equal to " + y);
  13. }else if(x>y){
  14.     System.out.println(x+ " is greater than " + y);
  15. }else{
  16.     System.out.println(x+ " is less than " + y);
  17. }
  18. }

Output:
  1. 37 is equal to 37


Java program to find leap year or not in java using if else


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.     int year = 2015;                      
  9.   //if year is divisible by 4, it is a leap year

  10.  if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
  11.  System.out.println("Year " + year + " is a leap year");
  12.  else
  13.       System.out.println( year + " is not a leap year");
  14.   
  15.  
  16. }

Output:
  1. 2015 is not a leap year


Java program to find even or odd using if else statment


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseEvenOddExample { 
  5.  
  6. public static void main(String[] args) {
  7.   
  8. int a=10;
  9. if((a%2)==0){
  10.  
  11.  System.out.println(a+"is even number");
  12.  
  13. }else{ 

  14.     System.out.println(a+"is odd number");
  15.  
  16. }
  17.  
  18. }

Output:
  1. 10 is even number

Read: If Else Statment in Java

Topic: Eclipse&#039;s latest release train carries Docker, Java technologies Previous Topic   Next Topic Topic: Atom at 1.0: GitHub&#039;s Node-based editor is just getting started

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use