GCGrowCarrer

Conditionals

Locked

Lesson 5 of 60 · Syntax · Easy

Finish these first:Operators and Expressions →Or pass the tests below to unlock it now.

1

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 `{ ... }`.
Key words (tap to learn):
2

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.

3

Quick check

In Java, where does the condition go in an `if` statement?

4

Your challenge

1 sample test, 1 hidden

Inside the { ... } block, write an if statement: when age >= 18, set status = "adult";.

Stuck? Get a hint (free)
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.

Mastery0%

Learn this first

Opens up next