Hide chapters

Dart Apprentice: Fundamentals

First Edition · Flutter · Dart 2.18 · VS Code 1.71

Dart Apprentice: Fundamentals

Section 1: 16 chapters
Show chapters Hide chapters

5. Control Flow
Written by Jonathan Sande

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

When writing a computer program, you need to be able to tell the computer what to do in different scenarios. For example, a calculator app performs one action if the user taps the addition button and another if they tap the subtraction button.

In computer programming terms, this concept is known as control flow, because you can control the flow of decisions the code makes at multiple points. In this chapter, you’ll learn how to make decisions in your programs.

Making Comparisons

You’ve already encountered a few different Dart types, such as int, double and String. Each of those types is a data structure that’s designed to hold a particular type of data. The int type is for integers while the double type is for decimal numbers. String, by comparison, is useful for storing textual information.

A new way of structuring information, though, requires a new data type. Consider the answers to the following questions:

  • Is the door open?
  • Do pigs fly?
  • Is that the same shirt you were wearing yesterday?
  • Is the traffic light red?
  • Are you older than your grandmother?
  • Does this make me look fat?

These are all yes-no questions. If you want to store the answers in a variable, you could use strings like 'yes' and 'no'. You could even use integers where 0 means no and 1 means yes. The problem with that, though, is what happens when you get 42 or 'celery'? It would be better to avoid any ambiguity and have a type in which the only possible values are yes and no.

Boolean Values

Dart has a data type just for this. It’s called bool, which is short for Boolean. A Boolean value can have one of two states. While in general, you could refer to the states as yes and no, on and off, or 1 and 0, most programming languages, Dart included, call them true and false.

const bool yes = true;
const bool no = false;
const yes = true;
const no = false;

Boolean Operators

Booleans are commonly used to compare values. For example, you may have two values and you want to know if they’re equal. Either they are equal, which would be true, or they aren’t equal, which would be false.

Testing Equality

You can test for equality using the equality operator, which is denoted by ==, that is, two equals signs.

const doesOneEqualTwo = (1 == 2);
const doesOneEqualTwo = 1 == 2;

Testing Inequality

You can also find out if two values are not equal using the != operator:

const doesOneNotEqualTwo = (1 != 2);
const alsoTrue = !(1 == 2);

Testing Greater and Less Than

There are two other operators to help you compare two values and determine if a value is greater than (>) or less than (<) another value. You know these from mathematics:

const isOneGreaterThanTwo = (1 > 2);
const isOneLessThanTwo = (1 < 2);
print(1 <= 2); // true
print(2 <= 2); // true
print(2 >= 1); // true
print(2 >= 2); // true

Boolean Logic

Each of the examples above tests just one condition. When George Boole invented the Boolean, he had much more planned for it than these humble beginnings. He invented Boolean logic, which lets you combine multiple conditions to form a result.

AND Operator

Ray would like to go cycling in the park with Vicki this weekend. It’s a little uncertain whether they can go, though. There’s a chance that it might rain. Also, Vicky says she can’t go unless she finishes up the art project she’s working on. So Ray and Vicki will go cycling in the park if it’s sunny and Vicki finishes her work.

const isSunny = true;
const isFinished = true;
const willGoCycling = isSunny && isFinished;

OR Operator

Vicki would like to draw a platypus, but she needs a model. She could either travel to Australia or she could find a photograph on the internet. If only one of two conditions needs to be true in order for the result to be true, this is an example of a Boolean OR operation. The only instance where the result would be false is if both input Booleans were false. If Vicki doesn’t go to Australia and she also doesn’t find a photograph on the internet, then she won’t draw a platypus.

const willTravelToAustralia = true;
const canFindPhoto = false;
const canDrawPlatypus = willTravelToAustralia || canFindPhoto;

Operator Precedence

As was the case in the Ray and Vicki examples above, Boolean logic is usually applied to multiple conditions. When you want to determine if two conditions are true, you use AND, while if you only care whether one of the two conditions is true, you use OR.

const andTrue = 1 < 2 && 4 > 3;
const andFalse = 1 < 2 && 3 > 4;
const orTrue = 1 < 2 || 3 > 4;
const orFalse = 1 == 2 || 3 == 4;
3 > 4 && 1 < 2 || 1 < 4
false && true || true
! >= > <= < == != && ||

false && true || true

Overriding Precedence With Parentheses

If you want to override the default operator precedence, you can put parentheses around the parts Dart should evaluate first.

3 > 4 && (1 < 2 || 1 < 4)  // false
(3 > 4 && 1 < 2) || 1 < 4  // true

String Equality

Sometimes you’ll want to determine if two strings are equal. For example, a children’s game of naming an animal in a photo would need to determine if the player answered correctly.

const guess = 'dog';
const guessEqualsCat = guess == 'cat';


  1. Create a constant called myAge and set it to your age. Then, create a constant named isTeenager that uses Boolean logic to determine if the age denotes someone in the age range of 13 to 19.
  2. Create another constant named maryAge and set it to 30. Then, create a constant named bothTeenagers that uses Boolean logic to determine if both you and Mary are teenagers.
  3. Create a String constant named reader and set it to your name. Create another String constant named ray and set it to 'Ray Wenderlich'. Create a Boolean constant named rayIsReader that uses string equality to determine if reader and ray are equal.

The If Statement

The first and most common way of controlling the flow of a program is through the use of an if statement, which allows the program to do something only if a certain condition is true. For example, consider the following:

if (2 > 1) {
  print('Yes, 2 is greater than 1.');
Yes, 2 is greater than 1.

The Else Clause

You can extend an if statement to provide code to run in the event that the condition turns out to be false. This is known as an else clause.

const animal = 'Fox';
if (animal == 'Cat' || animal == 'Dog') {
  print('Animal is a house pet.');
} else {
  print('Animal is not a house pet.');
Animal is not a house pet.

Else-If Chains

You can go even further with if statements. Sometimes you want to check one condition, and then check another condition if the first condition isn’t true. This is where else-if comes into play, nesting another if statement in the else clause of a previous if statement.

const trafficLight = 'yellow';
var command = '';
if (trafficLight == 'red') {
  command = 'Stop';
} else if (trafficLight == 'yellow') {
  command = 'Slow down';
} else if (trafficLight == 'green') {
  command = 'Go';
} else {
  command = 'INVALID COLOR!';
Slow down

Variable Scope

if statements introduce a new concept called scope. Scope is the extent to which a variable can be seen throughout your code. Dart uses curly braces as the boundary markers in determining a variable’s scope. If you define a variable inside a pair of curly braces, then you’re not allowed to use that variable outside of those braces.

const global = 'Hello, world';

void main() {
  const local = 'Hello, main';

  if (2 > 1) {
    const insideIf = 'Hello, anybody?';


  print(insideIf); // Not allowed!
Undefined name 'insideIf'.

The Ternary Conditional Operator

You’ve worked with operators that have two operands. For example, in (myAge > 16), the two operands are myAge and 16. But there’s also an operator that takes three operands: the ternary conditional operator. It’s related to if statements — you’ll see why this is in just a bit.

const score = 83;

String message;
if (score >= 60) {
  message = 'You passed';
} else {
  message = 'You failed';
(condition) ? valueIfTrue : valueIfFalse;
const score = 83;
const message = (score >= 60) ? 'You passed' : 'You failed';


  1. Create a constant named myAge and initialize it with your age. Write an if statement to print out “Teenager” if your age is between 13 and 19, and “Not a teenager” if your age is not between 13 and 19.
  2. Use a ternary conditional operator to replace the else-if statement that you used above. Set the result to a variable named answer.

Switch Statements

An alternate way to handle control flow, especially for multiple conditions, is with a switch statement. The switch statement takes the following form:

switch (variable) {
  case value1:
    // code
  case value2:
    // code


    // code

Replacing Else-If Chains

Using if statements are convenient when you have one or two conditions, but the syntax can be a little verbose when you have a lot of conditions. Check out the following example:

const number = 3;
if (number == 0) {
} else if (number == 1) {
} else if (number == 2) {
} else if (number == 3) {
} else if (number == 4) {
} else {
  print('something else');
const number = 3;
switch (number) {
  case 0:
  case 1:
  case 2:
  case 3:
  case 4:
    print('something else');

Switching on Strings

A switch statement also works with strings. Try the following example:

const weather = 'cloudy';
switch (weather) {
  case 'sunny':
    print('Put on sunscreen.');
  case 'snowy':
    print('Get your skis.');
  case 'cloudy':
  case 'rainy':
    print('Bring an umbrella.');
    print("I'm not familiar with that weather.");
Bring an umbrella.

Enumerated Types

Enumerated types, also known as enums, play especially well with switch statements. You can use them to define your own type with a finite number of options.

const weather = 'I like turtles.';
enum Weather {

Naming Enums

When creating an enum in Dart, it’s customary to write the enum name with an initial capital letter, as Weather was written in the example above. The values of an enum should use lowerCamelCase unless you have a special reason to do otherwise.

Switching on Enums

Now that you have the enum defined, you can use a switch statement to handle all the possibilities, like so:

const weatherToday = Weather.cloudy;
switch (weatherToday) {
  case Weather.sunny:
    print('Put on sunscreen.');
  case Weather.snowy:
    print('Get your skis.');
  case Weather.cloudy:
  case Weather.rainy:
    print('Bring an umbrella.');
Bring an umbrella.

Enum Values and Indexes

Before leaving the topic of enums, there’s one more thing to note. If you try to print an enum, you’ll get its value:

// Weather.cloudy
final index = weatherToday.index;

Avoiding the Overuse of Switch Statements

Switch statements, or long else-if chains, can be a convenient way to handle a long list of conditions. If you’re a beginning programmer, go ahead and use them; they’re easy to use and understand.


Before moving on, here are some challenges to test your knowledge of control flow. It’s best if you try to solve them yourself, but solutions are available in the challenge folder if you get stuck.

Challenge 1: Find the Error

What’s wrong with the following code?

const firstName = 'Bob';
if (firstName == 'Bob') {
  const lastName = 'Smith';
} else if (firstName == 'Ray') {
  const lastName = 'Wenderlich';
final fullName = firstName + ' ' + lastName;

Challenge 2: Boolean Challenge

In each of the following statements, what is the value of the Boolean expression?

true && true
false || false
(true && 1 != 2) || (4 > 3 && 100 < 1)
((10 / 2) > 3) && ((10 % 2) == 0)

Challenge 3: Audio Enumerations

  1. Make an enum called AudioState and give it values to represent playing, paused and stopped states.
  2. Create a constant called audioState and give it an AudioState value.
  3. Write a switch statement that prints a message based on the value.

Key Points

  • The Boolean data type bool can represent true or false.
  • The comparison operators, all of which return a Boolean, are:
Ekuef == Wivo Ipoqewuy Hup Oliag Ronq nzer Fbaogaz kcak Nogl hhoy aq udoed Lhuakis xxov un iyiiw != < > <= >=

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now