Conditionals and Branching
LockedLesson 4 of 25 · Syntax · Easy
Finish these first:Operators and Comparisons →Or pass the tests below to unlock it now.
The big idea
An if statement lets C++ decide whether to run a block of code. Put the condition inside parentheses ( ...
Think of it like this: Think of an `if` statement like a checkpoint: if the test in `( ... )` is true, C++ runs the code inside `{ ... }`.
Read the lesson
An if statement lets C++ decide whether to run a block of code.
Put the condition inside parentheses ( ... ) and the action inside curly braces { ... }:
int score = 92;
std::string grade = "";
if (score >= 90) {
grade = "A";
}
When updating a variable that already exists, just write grade = "A"; — do not repeat std::string.
Quick check
How do you write an `if` check in C++?
Your challenge
Inside main(), write an if check: when age >= 18, set status = "adult";.
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to python.conditionals (92% overlap). Branching control flow: if/else and switch/match expressions.
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Conditionals and Branching.
Learn this first
- Operators and ComparisonsNot yet