Skip to main content

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

Decision making: if-else

  1. We start with the variable age with a value of 25
  2. Then we have a condition to check if the age is greater than or equal to 18:
    • If age >= 18 resolves true (yes), then we will print adult.
    • If age >= 18 resolves false (no), then we will print underage.
  3. 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?