Dart Control Flow Statements: Complete Guide with Examples and Outputs
Control Flow Statements are one of the most important concepts in Dart programming. They determine how your program makes decisions, repeats tasks, and controls the execution of code. Every Flutter developer should have a strong understanding of these concepts because they are used in almost every application.
What is Control Flow?
Control Flow refers to the order in which the statements of a program are executed. By default, Dart executes code from top to bottom. However, using control flow statements, we can make decisions, execute code repeatedly, or skip certain parts of the program.
1. If Statement
The if statement executes a block of code only when a specified condition is true.
Syntax
if (condition) {
// code
}
Example
int age = 18;
if (age >= 18) {
print("Eligible to vote");
}
Output
Eligible to vote
In this example, the condition is true, so the code inside the if block gets executed.
2. If-Else Statement
The if-else statement is used when there are two possible outcomes.
Example
int age = 16;
if (age >= 18) {
print("Adult");
} else {
print("Minor");
}
Output
Minor
Since the condition is false, the else block executes.
3. Else-If Ladder
The else-if ladder allows multiple conditions to be checked one after another.
Example
int marks = 85;
if (marks >= 90) {
print("Grade A");
} else if (marks >= 80) {
print("Grade B");
} else if (marks >= 70) {
print("Grade C");
} else {
print("Fail");
}
Output
Grade B
Dart checks conditions from top to bottom and executes the first matching block.
4. For Loop
A for loop is used when the number of iterations is known in advance.
Example
for (int i = 1; i <= 5; i++) {
print(i);
}
Output
1 2 3 4 5
The loop starts from 1 and continues until the condition becomes false.
5. While Loop
The while loop executes as long as a condition remains true.
Example
int i = 1;
while (i <= 5) {
print(i);
i++;
}
Output
1 2 3 4 5
The condition is checked before each iteration.
6. Do-While Loop
The do-while loop executes at least once, even if the condition is false.
Example
int i = 5;
do {
print(i);
} while (i < 3);
Output
5
The code executes once before checking the condition.
7. Break Statement
The break statement immediately terminates a loop.
Example
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
print(i);
}
Output
1 2
When i becomes 3, the loop stops completely.
8. Continue Statement
The continue statement skips the current iteration and moves to the next one.
Example
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
print(i);
}
Output
1 2 4 5
The value 3 is skipped while the loop continues executing.
9. Switch Case
The switch statement is used when there are multiple fixed choices.
Example
String day = "Monday";
switch (day) {
case "Monday":
print("Start of Week");
break;
case "Friday":
print("Weekend Coming");
break;
default:
print("Normal Day");
}
Output
Start of Week
Switch statements improve readability when checking multiple constant values.
10. Ternary Operator ( ? : )
The ternary operator is a shorthand version of if-else.
Example
int age = 20;
String result =
age >= 18 ? "Adult" : "Minor";
print(result);
Output
Adult
This is useful when assigning values based on a condition.
11. Null Coalescing Operator ( ?? )
The null coalescing operator provides a default value when a variable is null.
Example
String? name; print(name ?? "Guest");
Output
Guest
This operator is very common in Flutter applications for handling nullable values.
When Should You Use Which Statement?
| Statement | Purpose |
|---|---|
| if | Single condition check |
| if-else | Two possible outcomes |
| else-if | Multiple conditions |
| for | Known number of iterations |
| while | Unknown iterations |
| do-while | Execute at least once |
| break | Stop loop execution |
| continue | Skip current iteration |
| switch | Multiple fixed choices |
| ?: | Short if-else |
| ?? | Default value for null |
Conclusion
Control Flow Statements are the foundation of Dart programming. They help developers make decisions, repeat tasks, and write efficient code. Whether you are preparing for Flutter interviews or building real-world applications, mastering if-else statements, loops, switch cases, and conditional operators will significantly improve your coding skills.
Practice these concepts regularly and try combining multiple control flow statements to solve real-world programming problems.

