Java if else statement
Java Tutorial » Java if else
In Java programming as many others programs, we use if statement to control the flow of the program created
Java programming has the following conditional statements:
1. if statement - block of code to be executed, if a specified condition is true,
2. if-else statement - a block of code to be executed, if the same condition is false,
3. if-else-if ladder - to specify a new condition to test, if the first condition is false,
4. nested if statement - to use if..else statements inside an if...else statement
if Statement, example
It will executes the "if' statement block if condition is true.
Syntax
Example
The number is greater.
if(condition){
//code to be executed if the condition is true
}
//code to be executed if the condition is true
}
Example
public class Main {
public static void main(String[] args) {
int my_number1 = 12;
int my_number2 = 7;
// checks if my_number1 is greater than my_number2
if (my_number1 > my_number2) {
System.out.println("The number is greater.");
}
}
}
Output:public static void main(String[] args) {
int my_number1 = 12;
int my_number2 = 7;
// checks if my_number1 is greater than my_number2
if (my_number1 > my_number2) {
System.out.println("The number is greater.");
}
}
}
The number is greater.
Java if-else Statement
The program will executes the if block, if condition is true otherwise else block will be executed
Syntax:
Example:
Have a good night.
if(condition){
//code executed if condition is true
}else{
//code executed if condition above if is false
}
//code executed if condition is true
}else{
//code executed if condition above if is false
}
Example:
public class Main {
public static void main(String[] args) {
int time = 21;
if (time > 22) {
System.out.println("Have a good evening.");
} else {
System.out.println("Have a good night.");
}
}
}
Output:public static void main(String[] args) {
int time = 21;
if (time > 22) {
System.out.println("Have a good evening.");
} else {
System.out.println("Have a good night.");
}
}
}
Have a good night.
Java if...else...if Statement
We can have "if...else...if" sequence, what can be used to execute a block of code among multiple other blocks
Syntax:
Example:
The number is 5.
if(con1){
//execut code if cond1 is true
}else if(con2){
//execut code if con2 is true
}
else if(con3){
//execut code if con3 is true
}
...
else{
//execut code if all the conditions are false
}
//execut code if cond1 is true
}else if(con2){
//execut code if con2 is true
}
else if(con3){
//execut code if con3 is true
}
...
else{
//execut code if all the conditions are false
}
Example:
public class Main {
public static void main(String[] args) {
int my_number = 5;
// checks if number is greater than 5
if (my_number > 5) {
System.out.println("The number is greater than 5.");
}
// checks if number is less than 5
else if (my_number < 5) {
System.out.println("The number is less than 5.");
}
// if both condition is false
else {
System.out.println("The number is 5.");
}
}
}
Output:public static void main(String[] args) {
int my_number = 5;
// checks if number is greater than 5
if (my_number > 5) {
System.out.println("The number is greater than 5.");
}
// checks if number is less than 5
else if (my_number < 5) {
System.out.println("The number is less than 5.");
}
// if both condition is false
else {
System.out.println("The number is 5.");
}
}
}
The number is 5.
Java Nested if..else Statement
In Java programms, it is also possible to use if..else inside an if...else statement.
Example:
public class Main {
public static void main(String[] args) {
// double type variables
Double a1 = -1.0, a2 = 4.5, a3 = -5.3, largest;
// checks if a1 greater than or equal to a2
if (a1 >= a2) {
// if...else statement inside the if block
// checks if a1 is greater than or equal to a3
if (a1 >= a3) {
largest = a1;
}
else {
largest = a3;
}
} else {
// if..else statement inside else block
// checks if a2 is greater than or equal to a3
if (a2 >= a3) {
largest = a2;
}
else {
largest = a3;
}
}
System.out.println("Biggest Number: " + largest);
}
}
public static void main(String[] args) {
// double type variables
Double a1 = -1.0, a2 = 4.5, a3 = -5.3, largest;
// checks if a1 greater than or equal to a2
if (a1 >= a2) {
// if...else statement inside the if block
// checks if a1 is greater than or equal to a3
if (a1 >= a3) {
largest = a1;
}
else {
largest = a3;
}
} else {
// if..else statement inside else block
// checks if a2 is greater than or equal to a3
if (a2 >= a3) {
largest = a2;
}
else {
largest = a3;
}
}
System.out.println("Biggest Number: " + largest);
}
}
java if else, if else one line, if condition, if statement, if ternario, if or, if else ejemplo, if string, if null, if not, program exeample, lambda, switch, multiple choice questions
Java if else statement - java
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.