O'Reilly logo

Real World XML by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required