In this lesson of our Java course, we will learn about the Java if else statement. Java is an object-oriented programming language that supports various control structures to control the flow of execution of a program. One of the most commonly used control structures in Java is the if-else statement. The Java if-else statement is a decision-making statement that allows a program to make a decision based on a condition.
Table of Contents
1. Java if else Statement
In this article, we will take a closer look at the “if-else” statement in Java. The “Java if-else” statement is an important control flow statement that allows developers to decide in their code. It is used to determine whether a particular block of code should be executed or not, depending on the outcome of a condition.
2. Understanding Java if Statement
The if statement in Java is used to check if a condition is true or false. If the condition is true, the statements inside the if block will be executed. If the condition is false, the statements inside the if block will be skipped and the program will continue execution with the next statement after the if block. Let’s look at the following syntax to understand it.
if (condition) {
// statements
}
3. How if statement works in Java
To understand how the java if statement works, let’s look at the following flowchart:
3.1. Java if Example
Let’s look at the following example to see how the if statement works in Java if else block.
public class IfExample {
public static void main(String[] args) {
int age = 30;
if (age >= 18) {
System.out.println("You are an adult.");
}
}
}
In this example, we have a variable age that is initialized to 30. Then, we have an if statement that checks if the value of age is greater than or equal to 18. If this condition is true, the code within the if block will be executed, which in this case is simply a System.out.println()
statement that prints “You are an adult.”
4. Java if else Condition
The if-else statement in Java is an extension of the if statement. It allows a program to make a decision based on a condition and execute a specific block of code if the condition is true and execute another block of code if the condition is false. Let’s look at the java if else statement:
if (condition) {
// codes in if block
}
else {
// codes in else block
}
5. How If else statement works?
To understand how Java if else statement works, let’s look at the following flowchart:
5.1. If else Example
public class JavaIfElse {
public static void main(String[] args) {
int age = 20;
if (age < 18) {
System.out.println("You are not an adult.");
} else {
System.out.println("You are an adult."); // else statement will execute
}
}
}
In the above example, the program checks the value of the “age
” variable and if it is less than 18
, it prints “You are not an adult.” If the value of the “age
” variable is greater than or equal to 18
, it prints “You are an adult.”
Summary
The “if-else” statement in Java is a powerful control flow statement that allows developers to decide in their code based on the outcome of a condition. It is a fundamental part of any programming language and is widely used in various applications. As always, the source code for this lesson is available on our Github Repository.