Our first example demonstrates branching with if and else in Java.
To run the program, compile it and then use java:
Note that in Java, you need parentheses around conditions, and the braces are required for multi-line blocks but optional for single-line blocks.
Unlike some languages, Java does have a ternary operator (?:), which can be used for simple conditional expressions. However, for more complex conditions, a full if statement is necessary.