Conditional Statements in Java

Conditional statements are used to compare two data types of values in Java. You can use if, if…else and switch statements for this purpose.

If and If…else

These statements compare values and follow the steps based on whether the comparison is true or false.

Example:

Output

Switch

The switch statement also compares values and follow the instructions based on when they are true.

Example

Output:

Switch can compare byte, short, int and char, and in Java 7 string is also supported.