If Statements – Basics
Write decisions with if, else if, else. Combine comparisons with logical operators and avoid common mistakes.
Decisions Overview
Decisions with if
Programs often need to choose a path based on data. In Java, if evaluates a boolean expression and executes a block when it is true.
- Comparison:
== != > < >= <= - Logical:
&&(and),||(or),!(not) - Blocks are inside braces
{ }.
If Syntax
Basic syntax
The condition in parentheses must be true or false.
public class Main {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("Adult");
}
}
}
All lessons in this course
- If Statements – Basics
- If Statements – Practice & Nested
- Common Pitfalls & Debugging Messages