Java if Statement

The if statement in Java is used to execute a block of code if a specified condition is true. The general syntax of an if statement is as follows:

refer t‮gi:o‬iftidea.com
if (condition) {
    // block of code to be executed if the condition is true
}

In this syntax, condition is a boolean expression that evaluates to either true or false. If the condition is true, the block of code inside the curly braces {} is executed. If the condition is false, the block of code is skipped.

Here is an example that uses an if statement to check if a number is positive:

int num = 5;
if (num > 0) {
    System.out.println(num + " is a positive number");
}

In this example, the condition num > 0 checks whether num is greater than 0, which is true in this case. Therefore, the block of code inside the curly braces {} is executed, and the output is:

5 is a positive number

If num were a negative number or zero, the condition would be false and the block of code would be skipped.