Control flow statements are fundamental in programming as they allow you to dictate the flow of your program's execution. In Dart, control flow statements include conditional statements, loops, and other constructs that help manage the execution path of your code.

Key Concepts

  1. Conditional Statements

    • if and else
    • switch and case
  2. Loops

    • for loop
    • while loop
    • do-while loop
  3. Other Control Flow Constructs

    • break
    • continue
    • return

Conditional Statements

if and else

The if statement allows you to execute a block of code based on a condition. The else statement can be used to execute a block of code if the condition is false.

void main() {
  int number = 10;

  if (number > 0) {
    print('The number is positive.');
  } else {
    print('The number is not positive.');
  }
}

switch and case

The switch statement allows you to execute one block of code among many options. It is often used as an alternative to multiple if-else statements.

void main() {
  int day = 3;

  switch (day) {
    case 1:
      print('Monday');
      break;
    case 2:
      print('Tuesday');
      break;
    case 3:
      print('Wednesday');
      break;
    default:
      print('Invalid day');
  }
}

Loops

for Loop

The for loop is used to execute a block of code a specific number of times.

void main() {
  for (int i = 0; i < 5; i++) {
    print('Iteration $i');
  }
}

while Loop

The while loop executes a block of code as long as a specified condition is true.

void main() {
  int i = 0;

  while (i < 5) {
    print('Iteration $i');
    i++;
  }
}

do-while Loop

The do-while loop is similar to the while loop, but it guarantees that the block of code will be executed at least once.

void main() {
  int i = 0;

  do {
    print('Iteration $i');
    i++;
  } while (i < 5);
}

Other Control Flow Constructs

break

The break statement is used to exit a loop or switch statement prematurely.

void main() {
  for (int i = 0; i < 5; i++) {
    if (i == 3) {
      break;
    }
    print('Iteration $i');
  }
}

continue

The continue statement skips the current iteration of a loop and proceeds to the next iteration.

void main() {
  for (int i = 0; i < 5; i++) {
    if (i == 3) {
      continue;
    }
    print('Iteration $i');
  }
}

return

The return statement is used to exit a function and optionally return a value.

int add(int a, int b) {
  return a + b;
}

void main() {
  int sum = add(3, 4);
  print('Sum: $sum');
}

Practical Exercises

Exercise 1: FizzBuzz

Write a program that prints the numbers from 1 to 20. For multiples of three, print "Fizz" instead of the number, and for the multiples of five, print "Buzz". For numbers that are multiples of both three and five, print "FizzBuzz".

void main() {
  for (int i = 1; i <= 20; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
      print('FizzBuzz');
    } else if (i % 3 == 0) {
      print('Fizz');
    } else if (i % 5 == 0) {
      print('Buzz');
    } else {
      print(i);
    }
  }
}

Exercise 2: Factorial Calculation

Write a function that calculates the factorial of a given number using a for loop.

int factorial(int n) {
  int result = 1;
  for (int i = 1; i <= n; i++) {
    result *= i;
  }
  return result;
}

void main() {
  int number = 5;
  print('Factorial of $number is ${factorial(number)}');
}

Common Mistakes and Tips

  • Forgetting to update loop variables: Ensure that loop variables are updated correctly to avoid infinite loops.
  • Misplacing break and continue statements: Use these statements carefully to control the flow of loops.
  • Incorrect conditions in if statements: Double-check conditions to ensure they are logically correct.

Conclusion

Control flow statements are essential for managing the execution path of your Dart programs. By mastering conditional statements, loops, and other control flow constructs, you can write more efficient and readable code. Practice the exercises provided to reinforce your understanding and prepare for more advanced topics in Dart programming.

Flutter Development Course

Module 1: Introduction to Flutter

Module 2: Dart Programming Basics

Module 3: Flutter Widgets

Module 4: State Management

Module 5: Navigation and Routing

Module 6: Networking and APIs

Module 7: Persistence and Storage

Module 8: Advanced Flutter Concepts

Module 9: Testing and Debugging

Module 10: Deployment and Maintenance

Module 11: Flutter for Web and Desktop

© Copyright 2024. All rights reserved