Flutter Tanzania

Posted on

# Control flow in Dart - Part 1

While building a program you will be in a situation where you want tell a computer to do different depend on the scenario. For example you want to check if user age is under 18 the give a message that he/she cannot vote and if user is above 18 he/she can vote.

So basically what we're doing here is we're controlling the flow of our program and telling it what to do depending on the scenario. Let's get it ðŸ¥³.

Comparing values

Referring to our example above, we want to know if user is above or under 18, say we have the following (17 > 18), what will answer be?, maybe yes or no?

Dart has a Data type called `bool` which is Boolean that has only to possible result `true` and `false`.

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.

Here's how we can define a bool variable

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

When comparing values in dart the result is always a boolean so let's learn more about it.

## Boolean operators

We mostly use bool values to compare.

Testing equality
In dart use can use `==` to test equality of two values. For example

``````(3 == 6)
``````

This will return a bool value of false.

Note: You may use the equality operator to compare int to double, since they both belong to the num type.

Testing inequality

If we use `==` we check if two values are equal, but what if we want to check if the values are not equal?, for this we can use `!=` and dart will return `true` if compared values are not equal and `false` if compared values are equal.

``````(3 != 6)
``````

Greater and less than

In other situation you want to check if a certain value is greater or is less than other value, we can do so by using `>` and `<`.

``````(3 > 6)
(3 < 6)
``````

This will return `false` and `true` respectively, but that's not the case, what if you want check if value is equal or grater than other and if is equal or less that, to perform this we will use `<=` for equal or less than and `>=` for equal or greater than

``````(3 >= 6)
(3 <= 6)
``````

But this are very minimal operators, we may be in a situation where we want to combine two or more condition.

For example we want to check if a student has finished his work and came to school early. We can do so by using `AND` operator.

``````const finishedHomeWork = true;
const isEarly = false;

const result = finishedHomeWork && isEarly;
``````

This code will combine two conditions and give us a result.

Stop ðŸ¤” what if we want check if a certain condition met or other condition is met and perform a task, to this we can use `OR` operator.

``````const finishedHomeWork = true;
const isEarly = false;

const result = finishedHomeWork || isEarly;
``````

Let's end here for this first section of control flow, in the next section we will learn about if statements and loops, how to perform certain task repetitive when a condition is met.