Hello! In the previous lesson, you calculated a purchase total and displayed the result. A program can also test that result: is the total within a budget? Is there enough stock? This lesson is about writing those tests as Boolean expressions—expressions that evaluate to true or false. You’ll build conditions now and use them to choose between actions in the next lesson. Allow about 35–40 minutes to read and run the examples.
Comparisons produce Boolean values
Suppose a purchase contains three items, but only two are in stock. The expression stock >= quantity asks whether the stock is greater than or equal to the quantity requested. With those values, its result is false.
Dart gives a Boolean result the type bool. You can store one just as you stored an arithmetic result:
void main() {
int stock = 2;
int quantity = 3;
bool enoughStock = stock >= quantity;
print('Enough stock: $enoughStock');
}
This prints Enough stock: false. The comparison has not changed either number; it has produced a new value.
Read the Equality and relational operators subsection of Dart’s official Operators guide for the six comparison operators. Its examples use assert(...) to check results; for now, focus on the expressions inside assert.
Read Dart’s official operator reference to see how equality and ordering comparisons are written.
In “Equality and relational operators,” read the comparison table, then look at the assert examples immediately below the explanation of ==. Stop before “Type test operators.” You can skip the intervening details about object identity, null, and operator methods; the immediate goal is comparing ordinary values.
Here is what all six operators say when stock and quantity are both 3:
| Expression | Question | Result |
|---|---|---|
stock == quantity |
Are they equal? | true |
stock != quantity |
Are they different? | false |
stock > quantity |
Is stock greater? | false |
stock < quantity |
Is stock less? | false |
stock >= quantity |
Is stock at least the quantity? | true |
stock <= quantity |
Is stock at most the quantity? | true |
The boundary matters: 3 > 3 is false, but 3 >= 3 is true. Use == to test equality; a single = assigns a value to a variable. Likewise, Dart writes “greater than or equal to” as >=, not =>.
Combine tests with AND, OR, and NOT
A purchase may require more than one check. Suppose it needs both enough stock and a total within budget. Dart’s && operator combines those Boolean expressions: its result is true only when both are true.
The logical operators are && (AND), || (OR), and ! (NOT). OR is true when at least one side is true—including when both sides are true. NOT reverses a Boolean value.
Read the Logical operators subsection of the same guide. Then briefly revisit its Operator precedence example to see why parentheses can make a combined condition easier to read.
Continue in Dart’s official Operators guide to learn how Boolean expressions combine and how they are grouped.
In “Logical operators,” read the logical-operator table and the if example directly beneath it. You need not learn if yet; notice how !, &&, ||, and parentheses form one condition. Stop before “Bitwise and shift operators.” Then, in “Operator precedence example,” read the precedence explanation and the two code examples directly below it. Stop before the warning about operator methods.
This complete program builds several conditions from one purchase. Save it as conditions.dart and run it with dart run conditions.dart:
void main() {
int stock = 2;
int quantity = 3;
int pricePerItem = 12;
int deliveryFee = 5;
int budget = 50;
int total = pricePerItem * quantity + deliveryFee;
bool enoughStock = stock >= quantity;
bool withinBudget = total <= budget;
bool canPurchase = enoughStock && withinBudget;
bool needsAttention = stock < quantity || total > budget;
bool cannotPurchase = !canPurchase;
print('Total: $total');
print('Enough stock: $enoughStock');
print('Within budget: $withinBudget');
print('Can purchase: $canPurchase');
print('Needs attention: $needsAttention');
print('Cannot purchase: $cannotPurchase');
}
The output is:
Total: 41
Enough stock: false
Within budget: true
Can purchase: false
Needs attention: true
Cannot purchase: true
The purchase is within budget, but there is not enough stock, so canPurchase is false: && requires both checks to pass. needsAttention is true because || needs only one reason to pass. Finally, !canPurchase reverses false to true.
Logical operators work with Boolean values, not numbers used as substitutes for Booleans. For example, quantity > 0 is a condition; quantity alone is an int, not a condition.
Make the intended grouping clear
When you mix operators, grouping affects the meaning. Consider a reservation rule: the total must be within budget, and the requested items must either be in stock or available to order later.
bool canReserve =
withinBudget && (enoughStock || acceptsBackOrder);
The parentheses keep the two ways of obtaining the items together. Dart evaluates comparisons before && and ||; it also evaluates && before ||. Without those parentheses, the condition would be grouped differently:
bool canReserveWithoutGrouping =
(withinBudget && enoughStock) || acceptsBackOrder;
That second rule could allow a reservation over budget whenever acceptsBackOrder is true. Parentheses are worth using even when you know the precedence rules: they show which parts of the condition belong together.
A condition may also include arithmetic, such as total <= budget where total was calculated earlier. Keep each question clear: calculate the value, compare it, then combine the resulting Boolean checks.
Key takeaways
Comparisons such as >= and == produce bool values. Use && when every condition must hold, || when at least one must hold, and ! to reverse a Boolean result. Parentheses make mixed conditions precise and readable.
You can now express when a purchase is possible. Next, you’ll use if and else to make a program take different actions depending on whether a condition is true or false.