Control Structures in Dart

In Dart, control structures are essential for managing the flow of execution in your programs. They allow you to make decisions, repeat actions, and handle different conditions. Understanding these control structures is crucial for writing efficient and effective Dart code. In this article, we’ll cover the main control structures in Dart: if statements, loops, and switch cases. Let’s dive into each of these in detail.

If Statements

The if statement is one of the most fundamental control structures in Dart. It allows you to execute a block of code conditionally, based on whether a certain expression evaluates to true or false.

Basic Syntax

if (condition) {
  // code to execute if the condition is true
}

Example

void main() {
  int number = 10;

  if (number > 5) {
    print('The number is greater than 5');
  }
}

In this example, the message will be printed because the condition number > 5 evaluates to true.

Else Clause

You can also use the else clause to execute a block of code when the condition is false.

void main() {
  int number = 3;

  if (number > 5) {
    print('The number is greater than 5');
  } else {
    print('The number is not greater than 5');
  }
}

Output:

The number is not greater than 5

Else If

When you need to check multiple conditions, you can use else if to add more conditions to the statement.

void main() {
  int number = 7;

  if (number > 10) {
    print('The number is greater than 10');
  } else if (number > 5) {
    print('The number is greater than 5 but less than or equal to 10');
  } else {
    print('The number is 5 or less');
  }
}

Output:

The number is greater than 5 but less than or equal to 10

Loops

Loops in Dart allow you to execute a block of code multiple times. There are several types of loops you can use, including for, while, and do-while.

For Loop

The for loop is commonly used when you know the number of iterations in advance:

Basic Syntax

for (initialization; condition; increment) {
  // code to execute
}

Example

void main() {
  for (int i = 0; i < 5; i++) {
    print('This is loop iteration number $i');
  }
}

Output:

This is loop iteration number 0
This is loop iteration number 1
This is loop iteration number 2
This is loop iteration number 3
This is loop iteration number 4

While Loop

The while loop continues executing as long as the specified condition is true.

Basic Syntax

while (condition) {
  // code to execute
}

Example

void main() {
  int count = 0;

  while (count < 5) {
    print('Count is $count');
    count++;
  }
}

Output:

Count is 0
Count is 1
Count is 2
Count is 3
Count is 4

Do-While Loop

The do-while loop is similar to the while loop but guarantees that the block of code will execute at least once, even if the condition is false initially.

Basic Syntax

do {
  // code to execute
} while (condition);

Example

void main() {
  int count = 0;

  do {
    print('Count is $count');
    count++;
  } while (count < 5);
}

Output:

Count is 0
Count is 1
Count is 2
Count is 3
Count is 4

Switch Case

The switch statement provides a way to select between multiple options based on the value of a variable. It is generally cleaner and more readable than a series of if-else statements when dealing with many potential conditions.

Basic Syntax

switch (expression) {
  case value1:
    // code to execute if expression equals value1
    break;
  case value2:
    // code to execute if expression equals value2
    break;
  default:
    // code to execute if expression does not match any case
}

Example

void main() {
  String day = 'Wednesday';

  switch (day) {
    case 'Monday':
      print('Start of the week!');
      break;
    case 'Wednesday':
      print('Midweek!');
      break;
    case 'Friday':
      print('Almost Weekend!');
      break;
    default:
      print('Just another day!');
  }
}

Output:

Midweek!

Fall Through Behavior

In Dart, the break statement is required to end each case; otherwise, Dart will execute the following case statements (known as fall-through behavior).

void main() {
  String day = 'Sunday';

  switch (day) {
    case 'Saturday':
    case 'Sunday':
      print('Weekend!');
      break;
    default:
      print('Weekday!');
  }
}

Output:

Weekend!

Conclusion

Mastering control structures in Dart is vital for building dynamic and responsive applications. The if statement allows you to make decisions, while loops help you repeat actions based on conditions, and the switch statement simplifies handling multiple choices.

In your Dart coding journey, practice using these structures, combining them in various scenarios, and observing how they can streamline your problem-solving processes. With these tools in your toolkit, you’re well on your way to becoming a proficient Dart programmer!