If statement :
Sample program on if statement:
Output:
Java program to compare two numbers in java
Output:
Else Statement:
Java program to compare two numbers in java using if else
Output:
Java program to find leap year or not in java using if else
Output:
Java program to find even or odd using if else statment
Output:
Control statements in java with examples
- 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){
- // statements
- }
- package com.instanceofjava;
- import java.lang.*;
- public class ifExample {
- public static void main(String[] args) {
- if(true){
- System.out.println("if condition executed");
- }
- }
Output:
- if condition executed
Java program to compare two numbers in java
- package com.instanceofjava;
- import java.lang.*;
- public class simpleIfExample {
- public static void main(String[] args) {
- int x= 37;
- int y= 37;
- if(x==y){
- System.out.println(x+ " is equal to " + y);
- }
- if(x>y){
- System.out.println(x+ " is greater than " + y);
- }
- if(x<y){
- System.out.println(x+ " is less than " + y);
- }
- }
Output:
- 37 is equal to 37
Else Statement:
- if(condition){ //} else { // }
Java program to compare two numbers in java using if else
- package com.instanceofjava;
- import java.lang.*;
- public class ifElseExample {
- public static void main(String[] args) {
- int x= 37;
- int y= 37;
- if(x==y){
- System.out.println(x+ " is equal to " + y);
- }else if(x>y){
- System.out.println(x+ " is greater than " + y);
- }else{
- System.out.println(x+ " is less than " + y);
- }
- }
Output:
- 37 is equal to 37
Java program to find leap year or not in java using if else
- package com.instanceofjava;
- import java.lang.*;
- public class ifElseExample {
- public static void main(String[] args) {
- int year = 2015;
- //if year is divisible by 4, it is a leap year
- if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
- System.out.println("Year " + year + " is a leap year");
- else
- System.out.println( year + " is not a leap year");
- }
Output:
- 2015 is not a leap year
Java program to find even or odd using if else statment
- package com.instanceofjava;
- import java.lang.*;
- public class ifElseEvenOddExample {
- public static void main(String[] args) {
- int a=10;
- if((a%2)==0){
- System.out.println(a+"is even number");
- }else{
- System.out.println(a+"is odd number");
- }
- }
Output:
- 10 is even number
Control statements in java with examples
No comments