Flutter: Decision making: if-else
if-else is a control statement that executes a block of code if a given condition is true
or another block of code if the condition is false
:
if (condition) {
// executed if the condition is true
} else {
// executed if the condition is false
}
The following flowchart example describes the if-else control statement:
Decision making: if-else
- We start with the variable
age
with a value of25
- Then we have a condition to check if the
age
is greater than or equal to18
:- If
age >= 18
resolvestrue
(yes), then we will printadult
. - If
age >= 18
resolvesfalse
(no), then we will printunderage
.
- If
- The program continues...
Let's run the code of DartPad:
After running in DartPad, adult
is printed in the console because the condition was true
. You can try changing the value of age
to a value lower than 18
. What is the result?