Control Flow Statements in Dart

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, and switch.
  • 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 >= 18 evaluates 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 >= 18 is false, the code inside else executes.

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:

  • case statements compare values.
  • break prevents fall-through.
  • default executes 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 = 1 initializes the loop.
  • i <= 5 is 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:

  • fruit represents each element in fruits.
  • 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 <= 5 is 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 &lt;= 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

  • break exits the nearest loop immediately.
  • continue skips 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 switch for multiple discrete values instead of long if-else-if chains.
  • Prefer for-in loops for collections; for loops for index-based iteration.
  • Comment complex logic for maintainability.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *