You are on page 1of 2

If Statement

In Java, an if statement is a conditional statement used to make decisions in your code.


It allows you to specify a block of code that should be executed if a given condition is
true. If the condition is false, the code inside the if block is skipped, and the program
continues with the next statements.

The basic syntax of an if statement in Java is as follows:

Here's a breakdown of the components:

1. if: The keyword that indicates the beginning of the if statement.


2. condition: A boolean expression or a value that can be evaluated as true or false.
3. {}: Curly braces that enclose the block of code to be executed if the condition is
true.

Here's an example of how you can use an if statement in Java:

In this example, if the value of the number variable is greater than 5, the message "The
number is greater than 5." will be printed to the console. If the condition is false, the
message will not be printed.

You can also use an else block to specify what should happen when the condition is
false:
In this case, if the number is less than or equal to 5, the message "The number is not
greater than 5." will be printed.

You might also like