Java if else is a conditional statement that is used when we want to execute a block of code based on some condition, the condition may evaluate to either true or false. If you dont know what are conditional statements kindly go through Java Operators tutorials.
Table of Contents
[hide]Java if is a conditional statement that executes the if block if a specified condition is true.
if (condition){ // executes this block if the condition evaluates to true}
Java is a case-sensitive language, hence if is in lowercase. Any other form would result in an error.
// Java program to illustrate the use of if statement
import java.util.*;
public class IfStatementExample{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter any amount ");
int amount= sc.nextInt();
if(amount > 1000 ){ // conditional check
System.out.println("Entered amount is greater than 1000");
}
}
}
In the above example we are taking the input from user, if you dont have any idea how to take input from the user then kindly have a look at Java User Input tutorial.
In the example we have entered the amount as 2000 that is greater than 1000, hence the condition evaluates to true and the if block will get executed. If you provide any input that is less than 1000, the condition will become false and it will not execute anything.
Java if else checks for the condition to be true or false. If block gets executed if the condition is true, and if the condition is false then else block is executed.
if (condition){ // executes this block if the condition evaluates to true} else { // executes this block if the condition evaluates to false}
// Java program to illustrate the use of if else statement
import java.util.*;
public class IfStatementExample{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter any amount ");
int amount= sc.nextInt();
if(amount > 1000 ){ // conditional check
System.out.println("Entered amount is greater than 1000");
} else {
System.out.println("Entered amount is less than 1000");
}
}
}
This example is same as that we have taken in the Java if statement. But here we have input the number 500 which is less than 1000, therefore Java else block gets executed.
// Java program to illustrate the use of if else statement
import java.util.*;
public class IfStatementExample{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter any num ");
int num= sc.nextInt();
if(num % 2 == 0 ){ // conditional check
System.out.println("Entered number is even");
} else {
System.out.println("Entered number is odd");
}
}
}
You can try the given programs in your system and test with various inputs.
Java if else if checks for the condition block by block. If the first condition is false, it will check the next condition and so on. As soon as a condition of a block evaluates to true that particular block gets executed.
if (condition1){ // executes this block if condition1 is true} else if (condition2) { // executes this block if condition1 is false and condition2 is true} else { // executes this block if all the above conditions are false}
// Java program to illustrate the use of if else if statement
import java.util.*;
public class IfElseIfExample{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter any value ");
int age= sc.nextInt();
if(age<=13) {// conditional check
System.out.println(" You are a Child ");
} else if (age >=13 && age <18) {
System.out.println(" You are a Teenager ");
} else if (age >=18) {
System.out.println(" You are an Adult ");
}
}
}
Try this example in your system with multiple inputs. Moreover you can write a program to find out Grades according to the marks obtained. Example one condition could be if marks < 40 then print fail.
Shorthand if else statements are also known as ternary operators because they consists of three operends. There are often used to replace simple if else statement by reducing the number of lines.
// Java program to illustrate the use of if else Shorthand statement
import java.util.*;
public class ShorthandExample {
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter marks obtained ");
int marks = sc.nextInt();
String result= (marks < 40) ? "Fail" : "Pass"; // Shorthand if else
System.out.println(" Final result is :"+ result);
}
}
Hope you have grasp the basics of Java if else through this tutorial. Practice more to get command over any topic.
In next tutorial we will learn about Java Switch Statements .