0Pricing
Dart Academy · Lesson

The Ternary and switch Expression

Pick values concisely without long if-chains.

The Ternary and switch Expression is a free Dart Academy lesson on CoddyKit — lesson 4 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.

Picking Values Concisely

Long if-chains can clutter your code. Dart offers shorter ways to choose a value based on a condition. Let us meet them. ✂️

The Ternary Operator

The ternary reads as condition ? valueIfTrue : valueIfFalse. It is a compact if-else that produces one of two values.

String label = isOn ? "On" : "Off";

Ternary Returns a Value

Unlike a plain if, the ternary is an expression, so you can drop it straight into a variable, an argument, or a return.

int max = a > b ? a : b;

Keep Ternaries Simple

A ternary shines for one quick choice. If you start nesting them deeply, reach for if-else or switch to stay readable.

var fee = isMember ? 0 : 10;

When You Have Many Cases

For more than two outcomes based on one value, the switch expression is clearer than a tall stack of else if branches.

The switch Expression

A modern switch expression matches a value against patterns with =>, and returns a result. Note there is no break needed here.

var name = switch (day) {
  1 => "Mon",
  2 => "Tue",
};

The Default With _

Use the wildcard _ as the catch-all case. It runs when nothing else matches, keeping your switch complete and safe.

var s = switch (code) {
  200 => "OK",
  _ => "Other",
};

switch Returns a Value Too

Like the ternary, a switch expression produces a value. You can assign its result directly instead of mutating a variable.

String icon = switch (status) {
  "win" => "🏆",
  _ => "❓",
};

Matching Several Values

Combine choices with || inside one case so several inputs share the same result. It keeps related cases together.

var kind = switch (n) {
  1 || 2 || 3 => "low",
  _ => "high",
};

Ternary vs switch

Reach for the ternary when there are exactly two outcomes, and a switch when one value maps to many possible results.

Concise and Readable

Used well, the ternary and switch expression replace bulky if-chains with code that reads almost like plain English. 🎯

Quick Check

Which syntax is the correct ternary operator in Dart?

Recap

You can now pick values fast: the ternary for two outcomes and the switch expression for many, both returning a value. 🎉

Frequently asked questions

Is the “The Ternary and switch Expression” lesson free?

Yes — the full text of “The Ternary and switch Expression” 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 “The Ternary and switch Expression”?

Pick values concisely without long if-chains. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Ternary and switch Expression” 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.

All lessons in this course

  1. Booleans and Comparison Operators
  2. if, else if, and else
  3. Logical Operators and Short-Circuiting
  4. The Ternary and switch Expression
← Back to Dart Academy