In this lesson of our Java course, we will learn about the Java if else. 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 given condition.
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 make decisions 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 given condition.
The if statement in Java is used to check if a given 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 the the following syntax to understand it.
if (condition) {
// statements
}
To understand how the java if statement works, let’s take a look at the following flowchart:
Let’s take a 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.”
The if-else statement in Java is an extension of the if statement. It allows a program to make a decision based on a given 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 take a look at the java if else statement:
if (condition) {
// codes in if block
}
else {
// codes in else block
}
To understand how Java if else statement works, let’s look at the following flowchart:
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.”
The “if-else” statement in Java is a powerful control flow statement that allows developers to make decisions in their code based on the outcome of a given 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.