In this tutorial, we'll learn about control flow statements using Java if and if...else statements. We'll learn how to use the if-else statement in Java with the help of examples.
Types of if statement
There are various types of if statement in Java.
These statements allow we to control the flow of our program’s execution based upon conditions.
if statement is the most simple decision making statement. The Java if statement tests the condition. It executes the if block if condition is true otherwise not.
Syntax:
if(condition){
// if condition is true
// code to be executed
}
// Java program to illustrate If statement
class Simple{
public static void main(String args[]){
// defining an age variable
int age = 20;
//checking the age
if(age >= 18){
System.out.println("You can vote");
}
}
}
Output:
You can vote
// Java program to illustrate If statement
class Simple{
public static void main(String args[]){
// defining crush variable
String crush = "Ankita";
//if statement
if(crush == "Ankita"){
System.out.println("Ankita is your crush");
}
}
}
Output:
Ankita is your crush
The java if-statement is also used to perform a task depending on whether a given condition is true or false.
It executes the if block if condition is true otherwise else block is executed.
Syntax:
if(condition){
// if condition is true
// code to be executed
}
else{
// if condition is false
// code to be executed
}
// Java program to illustrate If-else statement
class Simple{
public static void main(String args[]){
int number = 5;
//checks if number is greater than 0
if(number > 0){
System.out.println("The number is positive");
}
//if number is less than 0
// execute this block
else{
System.out.println("The number is negative");
}
}
}
Output:
The number is positive
class Simple{
public static void main(String args[]){
int number = 15;
//Check if the number is divisible by 2 or not
if(number % 2 == 0){
System.out.println("Even number");
}
//if the number is not divisible by 2
// execute this block
else{
System.out.println("Odd number");
}
}
}
Output:
Odd number
In Java, we have an if...else...if ladder, that can be used to execute one condition from multiple statements.
Syntax:
if(condition1){
// if condition1 is true
// code to be executed
}
else if(condition2){
// if condition2 is true
// code to be executed
}
else if(condition3){
// if condition3 is true
// code to be executed
}
...
else{
// if all conditions are false
// code to be executed
}
// Java program to illustrate If-else-if statement
// Java program to check a number is positive, negative or zero.
class Simple{
public static void main(String args[]){
int number = 15;
if(number > 0){
System.out.println("Positive");
}
else if(number < 0){
System.out.println("Negative");
}
else{
System.out.println("Zero");
}
}
}
Output:
Positive
In java the nested if statement represents the if block within another if block. if the outer if block condition is true, then the inner if block condition executes.
Syntax:
if(condition){
// if condition is true
// code to be executed
if(condition){
// if condition is true
// code to be executed
}
}
// Java program to illustrate If-else-if statement
class Simple{
public static void main(String args[]){
String username = "rj801";
int password = 12345;
if(username == "rj801"){
if(password == 12345){
System.out.println("Login sucessful");
}
else{
System.out.println("incorrect password");
}
else{
System.out.println("incorrect username");
}
}
}
Output:
Login sucessful