HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to DartVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Dart›Conditions (if / else)

Conditions (if / else)

Decision making with if-else if-else, branching using comparison and logical operators.

Simple enough for a 5-year-old to understand

making decisions with if

Example code
1
void main() {
2
var age = 20;
3
if (age >= 18) {
The condition is inside parentheses.
ConditionThe condition is inside parentheses.
4
print("Adult");
It runs if the condition is true.
Output / InputIt runs if the condition is true.
5
} else {
If not...
ConditionIf not...
6
print("Child");
...this runs.
Output / Input...this runs.
7
}
8
}
ConditionOutput / Input
💡 Hint: The condition is written inside parentheses ( ), and the block inside curly braces { }.

About this topic

Conditions in Dart are written with if, else if, else, and switch. The condition expression must be a bool; numbers or strings cannot be used as truth values.

This strictness prevents two mistakes at once. First, accidentally making an assignment in the condition causes a compilation error. Second, you don't need to memorize language-dependent rules like "zero is falsy"; you write your intent clearly.

The ternary operator is commonly used in Dart, especially in Flutter UI code. It's a shorthand for choosing between two values based on a condition, and when nested, it quickly ruins readability; if more than two branches are needed, an if should be preferred.

What you will learn

  • the if, else if, and else structure
  • The requirement for the condition to be a bool
  • Combining conditions with logical operators
  • Using switch
  • The ternary operator

Who is this for?

  • Learners of control flow
  • Those coming from JavaScript expecting truthy values
  • Those doing conditional rendering in the Flutter UI

When to use?

An if chain is sufficient for decisions with two or three states; if there is branching based on more constant values, switch is more readable.

When you need to show two different components based on a condition in the UI, the ternary operator is the shortest way.

Common mistakes

Wrong

void main() {
  var liste = [1, 2];
  if (liste.length) {
    print("full");
  }
}

Dart requires the condition to be a bool; it does not count a number as a truth value and throws "Conditions must have a static type of 'bool'". This pattern that works in JavaScript does not pass in Dart; you must write the comparison explicitly.

Correct

void main() {
  var liste = [1, 2];
  if (liste.isNotEmpty) {
    print("full");
  }
}

Wrong

void main() {
  var gun = "pazartesi";
  switch (gun) {
    case "pazartesi":
      print("start of the week");
    case "cuma":
      print("weekend");
  }
}

A non-empty case block in Dart cannot complete normally; you get the error "The 'case' shouldn't complete normally". Each branch must end with break, return, or a similar command. This rule makes accidental fall-through errors from C impossible.

Correct

void main() {
  var gun = "pazartesi";
  switch (gun) {
    case "pazartesi":
      print("start of the week");
      break;
    case "cuma":
      print("weekend");
      break;
  }
}

Wrong

void main() {
  String? ad;
  if (ad != null) {
    print(ad.length);
  }
  print(ad.length);
}

The first access is safe because Dart narrows the type after the check; the second access throws an error because it is outside the check block. Type promotion only works within the scope where the check is valid, a distinction that is easily overlooked.

Correct

void main() {
  String? ad;
  if (ad != null) {
    print(ad.length);
  } else {
    print(0);
  }
}

Frequently asked questions

Why is break mandatory in switch?

Because Dart wants to prevent the accidental fall-through error to the next branch found in C. If you intentionally want it to fall through, you can leave the branch empty or jump to a label with continue; but you must write this explicitly.

How does type promotion work?

Inside the block where you performed the null check, Dart treats the value as a non-nullable type. However, if the variable is a field that can change later, this promotion does not work; in such cases, assigning the value to a local variable first solves the problem.

When should I use the ternary operator?

When making a short choice between two options. Nested ternary operators quickly destroy readability; if more than two branches are needed, an if chain or switch should be preferred.

Summary

Conditions must be of type bool.

switch branches cannot fall through automatically; break is required.

The null check narrows the type only within that block.

The ternary operator is short for two options.

A number cannot be used as a truth value.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code