Getting Familiar with Dart

Control Flow


Learning Objectives

  • You know that all programming languages provide some mechanism for controlling the flow of the program.
  • You know the basic control flow structures, including conditional statements, switch statements, and loops.
  • You know of relational and logical operators.
  • You know how to use control flow structures in Dart.

All programming languages provide some mechanism for controlling the flow of the program. This includes deciding which code blocks to execute based on certain conditions, and repeating code blocks a certain number of times or until a certain condition is met. In Dart, like in many other programming languages, the basic control flow structures include conditional statements, switch statements, and loops.

On the CPU-level, control flow is implemented using instructions that allow the program to jump to a different location in the code. These instructions include branching instructions, conditional jumps, and jump tables. In higher-level programming languages, these instructions are abstracted away, and the programmer uses control flow structures to control the flow of the program.

Conditional statements

Conditional statements are given a boolean expression that evaluates into either true or false. If the expression evaluates into true, the code block following the if-statement is executed. If the expression evaluates into false, the code block following the else-statement is executed. The basic syntax of the if-statement is as follows.

Run the program to see the output

There can be an infinite number of else if-statements between the if-statement and the else-statement. The else-statement is optional, and the else if-statement can be used to check for additional conditions. The following example outlines a program that outputs information about water based on the temperature (here, we use Celsius).

Run the program to see the output

Conditional statements are used to make decisions in program code, while switch statements are used to check whether a given value equals a value in one of the given cases. Loops are used to repeat a block of code a number of times or until a certain condition is met.

Relational and logical operators

The abovementioned if-statements are often used with relational and logical operators. Relational operators are used to check for relationships between variable values. The basic relational operators are — again similar to other languages — as follows.

OperatorDescriptionExampleResult
<Smaller than5 < 3false
>Greater than5 > 3true
<=Smaller than or equal to3 <= 2false
>=Greater than or equal to3 >= 2true
==Equal to3 == 2false
!=Not equal to3 != 2true

Relational operators, combined with variable values, evaluate into a boolean (either true or false). Like in other programming languages, they are commonly used as a part of the if-statement. The following example outlines a program that outputs information about water based on the temperature (here, we use Celsius).

Run the program to see the output

Relational operators (and, expressions that produce a boolean in general) can be combined with logical operators. The key logical operators are && (and), || (or), and ! (not).

OperatorDescriptionExampleResult
&&And1 < 2 && 2 < 3true
||Or1 != 1 || 2 != 2false
!Not!(2 > 1)false

As an example, if we would wish to restrict access to an amusement park ride based on age and height, where only those who are at least seven years old and at least 120 centimeters tall can enter, we could use the && (and) operator in the evaluation.

Run the program to see the output

Loading Exercise...

Ternary operators

Ternary operators are shorthand conditional operators that consist of three parts. The first part is an expression that evaluates either into true or false, followed by a question mark. The second and third parts are expressions that are executed in the case the first expression evaluates into true or false, respectively.

The following program outlines an example of using ternary operators for determining output. In this case, the program outputs information on whether a sauna is ready.

Run the program to see the output

Switch statements and switch expressions

The switch statement is used to check whether a given value, often called a scrutinee, equals a value in one of the given cases. The switch keyword is followed by a value in parentheses, and the cases are defined with the case keyword. The default keyword is used to define the code block that is executed if none of the cases match the value.

Run the program to see the output

Some other languages like Java require the break keyword to exit the switch statement after a case is matched. In Dart, the break keyword is not required, as the program automatically exits the switch statement after a case is matched.

Dart comes also with switch expressions, which allow “returning” a value from the switch and case statements. The following example demonstrates the use of switch expressions to determine the compliment based on the grade.

Run the program to see the output

We will run into the switch expression many times on this coursee — particularly when we learn about matching in Gleam and Rust.

Loading Exercise...

For- and while-loops

Dart also comes with the usual for- and while-loops. The for-loop is used to repeat a block of code a number of times, while the while-loop is used to repeat a block of code as long as a given condition is true.

The following example demonstrates the use of a for-loop to print numbers from 1 to 5.

Run the program to see the output

The following example demonstrates the use of a while-loop to print numbers from 1 to 5.

Run the program to see the output

Both for and while loops must terminate at some point. If the condition in the loop never evaluates into false, the loop will continue indefinitely, causing the program to hang. This is known as an infinite loop.

Loading Exercise...