Conditional statements
Learning objectives
- You practice the basics of programming with Dart.
- You know the basics of creating conditional behavior with Dart.
Making decisions
Dart comes with the basic functionality needed to make decisions in program code or, in other words, to have multiple optional execution paths in the program. The basic control flow structures used to create optional execution paths are the if
-statement and the switch
-statement. Both of them are also present in many other programming languages.
The following examples outline the basic syntax of the statements. In the first eaxmple, we decide what to do based on the value of the boolean warmEnough
, while in the second example we decide on what to do based on the value of the string grade
.
The if
and switch
statements differ in that if
statements always work with booleans -- or at least with expressions that evaluate into booleans -- while switch
statements work by checking whether the given value equals a value in one of the given cases.
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.
Operator | Description | Example | Result |
---|---|---|---|
< | Smaller than | 5 < 3 | false |
> | Greater than | 5 > 3 | true |
<= | Smaller than or equal to | 3 <= 2 | false |
>= | Greater than or equal to | 3 >= 2 | true |
== | Equal to | 3 == 2 | false |
!= | Not equal to | 3 != 2 | true |
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).
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).
Operator | Description | Example | Result |
---|---|---|---|
&& | And | 1 < 2 && 2 < 3 | true |
|| | Or | 1 != 1 || 2 != 2 | false |
! | 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.
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.
As an example, the following program uses a ternary operator the print information about the ability to access an amusement park ride (similar to the previous example).
While the above example shows the use of ternary operators for printout output, ternary operators are more commonly used for assigning variable values. As an example, a value of a variable could depend on the values of other variables. This is demonstrated in the next example.
The following program outlines another example of using ternary operators for determining output. In this case, the program outputs information on whether a sauna is ready.