Introduction
Control flow statements are essential in programming—they allow your Dart program to make decisions and execute code repeatedly. In Dart, control flow statements include if-else, switch-case, and loops such as for, while, and do-while. Understanding these concepts is crucial for writing efficient, dynamic, and logical programs.
In this guide, we will cover:
- Conditional statements:
if,if-else,if-else-if, andswitch. - Loops:
for,for-in,while,do-while. - Nested and advanced control flow examples.
- Best practices and common pitfalls.
By the end, you will have a thorough understanding of Dart control flow statements.
Conditional Statements in Dart
1. The if Statement
The if statement evaluates a condition and executes code if the condition is true.
void main() {
int age = 18;
if (age >= 18) {
print('You are an adult.');
}
}
Explanation:
- Condition
age >= 18evaluates to true. - Code inside
{}runs only if the condition is true.
2. The if-else Statement
Use if-else to execute one block if the condition is true, and another if false.
void main() {
int age = 16;
if (age >= 18) {
print('You are an adult.');
} else {
print('You are a minor.');
}
}
Explanation:
- If
age >= 18is false, the code insideelseexecutes.
3. The if-else-if Ladder
For multiple conditions, use else if.
void main() {
int score = 75;
if (score >= 90) {
print('Grade: A');
} else if (score >= 75) {
print('Grade: B');
} else if (score >= 50) {
print('Grade: C');
} else {
print('Grade: F');
}
}
Explanation:
- Dart evaluates conditions in order.
- The first true condition executes, others are skipped.
4. The switch Statement
The switch statement evaluates an expression and executes code matching a case.
void main() {
String fruit = 'Apple';
switch (fruit) {
case 'Apple':
print('You chose Apple');
break;
case 'Banana':
print('You chose Banana');
break;
case 'Mango':
print('You chose Mango');
break;
default:
print('Unknown fruit');
}
}
Explanation:
casestatements compare values.breakprevents fall-through.defaultexecutes if no case matches.
Loops in Dart
1. The for Loop
Use for loops to repeat code a fixed number of times.
void main() {
for (int i = 1; i <= 5; i++) {
print('Iteration $i');
}
}
Explanation:
i = 1initializes the loop.i <= 5is the condition.i++increments the counter.
2. The for-in Loop
Iterates over elements in a collection like a list.
void main() {
var fruits = ['Apple', 'Banana', 'Mango'];
for (var fruit in fruits) {
print(fruit);
}
}
Explanation:
fruitrepresents each element infruits.- Loop automatically ends when all elements are traversed.
3. The while Loop
Repeats code as long as a condition is true.
void main() {
int i = 1;
while (i <= 5) {
print('Count: $i');
i++;
}
}
Explanation:
- Condition
i <= 5is evaluated before each iteration. - If false initially, the loop does not run.
4. The do-while Loop
Executes code at least once, then repeats while a condition is true.
void main() {
int i = 1;
do {
print('Count: $i');
i++;
} while (i <= 5);
}
Explanation:
- The loop body runs first.
- Condition is checked afterward.
Nested Control Flow
You can nest loops and conditionals to handle complex logic.
void main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
print('i=$i, j=$j');
}
}
}
- Outer loop (
i) and inner loop (j) create combinations. - Useful for tables, grids, and multi-level logic.
Using break and continue
breakexits the nearest loop immediately.continueskips the current iteration and continues to the next.
void main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip 3
}
print(i);
if (i == 4) {
break; // Stop loop at 4
}
}
}
Output:
1
2
4
Practical Examples
Example 1: Grading System
void main() {
int score = 82;
if (score >= 90) {
print('Grade A');
} else if (score >= 75) {
print('Grade B');
} else if (score >= 50) {
print('Grade C');
} else {
print('Grade F');
}
}
Example 2: Looping through a List
void main() {
var numbers = [10, 20, 30, 40];
for (var num in numbers) {
print(num * 2);
}
}
Example 3: Multiplication Table
void main() {
int num = 5;
for (int i = 1; i <= 10; i++) {
print('$num x $i = ${num * i}');
}
}
Best Practices
- Use clear indentation and braces
{}to improve readability. - Avoid deeply nested loops and conditionals; consider breaking into functions.
- Use
switchfor multiple discrete values instead of longif-else-ifchains. - Prefer
for-inloops for collections;forloops for index-based iteration. - Comment complex logic for maintainability.
Leave a Reply