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.

You may also like

LEAVE A COMMENT

Advertisements

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.