Back to quest
Dart Basics & Data TypesLevel 8 / 32

Booleans: True and False

Understanding boolean logic and comparison operators

The Boolean data type (bool) is the most basic yet powerful type in programming. It has only two possible values: true or false. Booleans are the decision-makers in your code, controlling the flow of logic based on conditions.

1. Declaring Booleans

Booleans are strictly typed in Dart. Unlike some other languages where 1 or 0 might count as true/false, in Dart, a boolean must explicitly be the keyword true or false.

Basic Declaration

example.dart
1// Explicit declaration
2bool isOn = true;
3bool isOff = false;
4 
5// Type inference (common in Dart)
6var isLoading = true;
7var hasError = false;
8 
9print(isOn); // true
10print(isOff); // false
11print(isOn.runtimeType); // bool

HintA boolean variable stores the state of a condition. It is either on or off, yes or no.

2. Comparison Operators

Booleans are often the result of comparing two values. Dart uses standard comparison operators to evaluate relationships between numbers or other data.

Making Comparisons

example.dart
1int score = 100;
2 
3// Equality operators
4print(score == 100); // true (Equal to)
5print(score != 50); // true (Not equal to)
6 
7// Relational operators
8print(score > 50); // true (Greater than)
9print(score < 50); // false (Less than)
10print(score >= 100); // true (Greater than or equal to)
11print(score <= 90); // false (Less than or equal to)
12 
13bool isWinner = score > 80;
14print('Is Winner: $isWinner'); // Is Winner: true

HintThe result of any comparison (like 5 > 3) is always a boolean value.

3. Logical Operators

You can combine multiple boolean conditions using logical operators: AND (&&), OR (||), and NOT (!).

Combining Logic

example.dart
1bool hasLicense = true;
2bool isOver18 = false;
3 
4// AND (&&): Both must be true
5bool canDrive = hasLicense && isOver18;
6print('Can drive: $canDrive'); // false
7 
8// OR (||): At least one must be true
9bool needsRide = !hasLicense || !isOver18;
10print('Needs ride: $needsRide'); // true
11 
12// NOT (!): Inverts the value
13bool isMinor = !isOver18;
14print('Is minor: $isMinor'); // true

HintUse && when you need strict compliance (all conditions met). Use || when any single condition is enough.

4. Common "Gotchas"

A classic mistake in many languages is confusing the assignment operator (=) with the equality operator (==).

Assignment vs. Equality

example.dart
1int x = 10;
2int y = 5;
3 
4// Correct: Comparing x and y
5bool isEqual = (x == y); // false
6 
7// Incorrect logic often seen:
8// if (x = y) ... // ❌ Error in Dart!
9// Dart prevents this because 'x = y' is an assignment, not a boolean expression.
10 
11print('Is x equal to y? $isEqual');

HintRemember: = sets a value. == checks a value.

Loot collected

Key takeaways
  • The bool type only accepts true or false.
  • Dart is type-safe: numbers (1, 0) are NOT automatically converted to booleans.
  • Comparison operators (==, !=, >, <) return boolean results.
  • Logical operators allow you to combine conditions: && (AND), || (OR), ! (NOT).
  • Don’t confuse = (assignment) with == (equality check).

Side quests

Practice exercises

1

Exercise 1: Simple Logic

Create a boolean variable called isSunny. Set it to true. Print "Is it sunny? " followed by the variable.

Reveal solution
solution.dart
1bool isSunny = true; print('Is it sunny? $isSunny');
2

Exercise 2: The Night Club Bouncer

Declare two variables: age (int) and hasID (bool). Create a boolean canEnter that is true ONLY if age is 21 or older AND hasID is true.

Reveal solution
solution.dart
1int age = 20; bool hasID = true; bool canEnter = (age >= 21) && hasID; print(canEnter);
3

Exercise 3: Opposite Day

Given bool isHappy = true;, use the NOT operator to print false.

Reveal solution
solution.dart
1print(!isHappy);
Party chat

Comments