Java Conditional Statements: if, if…else, switch

After operators, the next level up is to use conditional statements. Java supports the same conditional statements as JavaScript: if, if...else, and switch.

The if statement allows you to check a condition, which you create using the Java conditional operators, such as <, >, and ==:

if (condition) {
    code executed if condition is true
}
else {
    code executed if condition is false
}

For example, say you had two double variables, assets and debts, and you wanted to compare the two to make sure you're solvent. You might want the code to display a message, such as "You're solvent.". You can do that like this:

Listing . ch10_07.java
 public class ch10_07 { public static void main(String[] args) { double ...

Get Real World XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.