if, else if, and else
Run code only when conditions hold.
if, else if, and else is a free Dart Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Run Code Only Sometimes
Sometimes you want code to run only when a condition is met. The if statement is how you make that choice in Dart. 🔀
The Basic if
An if takes a bool in parentheses. When it is true, the block in braces runs; when false, Dart simply skips it.
if (score > 90) {
print("Great job!");
}The Condition Must Be a bool
Dart insists the condition inside if is a real bool. A number or string alone will not compile, unlike some other languages.
if (isLoggedIn) {
print("Welcome back");
}Add an else
Pair an else with if to handle the false case. Exactly one of the two blocks always runs, never both and never neither.
if (age >= 18) {
print("Adult");
} else {
print("Minor");
}Chain With else if
Need more than two paths? Stack else if branches. Dart checks them top to bottom and runs the first one that is true.
if (n > 0) {
print("positive");
} else if (n < 0) {
print("negative");
}First Match Wins
Order matters in a chain. Once a branch is true, Dart runs it and skips the rest, so put your most specific tests first.
if (score >= 90) grade = "A";
else if (score >= 80) grade = "B";A Final else Catches the Rest
End a chain with a plain else to catch every remaining case. It is your safe default when no earlier condition matched.
if (light == "red") stop();
else if (light == "green") go();
else wait();Braces Keep You Safe
You can skip braces for a single line, but always using braces prevents sneaky bugs when you add a second line later.
if (ok) {
print("line one");
print("line two");
}Nesting if Statements
You can place an if inside another to ask follow-up questions. Keep nesting shallow so your logic stays easy to read.
if (loggedIn) {
if (isAdmin) showPanel();
}Comparisons as Conditions
The comparisons you learned slot right into if. Any expression that yields a bool can steer which branch runs.
if (cart.length > 0) {
print("Ready to check out");
}if Is the Workhorse
From validating input to picking messages, if and its friends power most everyday decisions you will write in Dart. 💪
Quick Check
How many branches of an if-else chain actually run?
Recap
You can now branch with if, fall back with else, chain with else if, and trust that only the first true branch runs. 🎉
Frequently asked questions
Is the “if, else if, and else” lesson free?
Yes — the full text of “if, else if, and else” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “if, else if, and else”?
Run code only when conditions hold. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “if, else if, and else” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.