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:
- We start with the variable
agewith a value of25 - Then we have a condition to check if the
ageis greater than or equal to18:- If
age >= 18resolvestrue(yes), then we will printadult. - If
age >= 18resolvesfalse(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?
