The if statement in Java is used to make decisions. It allows a program to execute a block of code only when a specified condition evaluates to true.
The if statement checks a condition. If the condition is true, the code inside the block runs.
// Basic syntax of if statement
if (condition) {
// statements
}
// Program to check if a number is positive
int number = 10;
if (number > 0) {
System.out.println("Number is positive");
}
If the value of number is greater than 0, the message Number is positive will be printed.
if statement