Conditionals
LockedLesson 5 of 60 · Syntax · Easy
Finish these first:Operators and Expressions →Or pass the tests below to unlock it now.
The big idea
An if statement lets Java choose whether to run a block of code based on a true/false check. Put the condition inside parentheses ( ...
Think of it like this: Think of an `if` statement like a gatekeeper: if the condition inside `( ... )` is true, Java runs the code inside `{ ... }`.
Read the lesson
An if statement lets Java choose whether to run a block of code based on a true/false check.
Put the condition inside parentheses ( ... ) and the code to run inside curly braces { ... }:
int score = 92;
String grade = "";
if (score >= 90) {
grade = "A";
}
If the condition is true, Java runs the lines inside { ... }. If it is false, Java skips right over them.
Quick check
In Java, where does the condition go in an `if` statement?
Your challenge
Inside the { ... } block, write an if statement: when age >= 18, set status = "adult";.
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to python.conditionals (95% overlap). Same logic; Java uses braces and `&&`/`||`, Python uses indentation and `and`/`or`.
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Conditionals.
Learn this first
- Operators and ExpressionsNot yet