0 day streak
java / beginner
Snippet

The If-Else Statement

The if-else statement allows the program to make decisions by executing different blocks of code based on whether a condition is true or false.

snippet.java
java
1
2
3
4
5
6
int age = 20;
if (age >= 18) {
System.out.println("Access granted");
} else {
System.out.println("Access denied");
}
Breakdown
1
if (age >= 18)
Checks if the age variable is 18 or greater.
2
System.out.println("Access granted");
This line runs if the condition is true.
3
} else {
Defines what happens if the condition is false.
4
System.out.println("Access denied");
This line runs if the condition is false.