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 switch statements. This section will cover the following topics:

  1. Conditional Statements
    • if and else
    • else if
    • Ternary Operator
  2. Loops
    • for loop
    • while loop
    • do-while loop
  3. Switch Statements

  1. 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.

Syntax:

if (condition) {
  // code to be executed if condition is true
} else {
  // code to be executed if condition is false
}

Example:

void main() {
  int number = 10;

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

else if

The else if statement allows you to check multiple conditions.

Syntax:

if (condition1) {
  // code to be executed if condition1 is true
} else if (condition2) {
  // code to be executed if condition2 is true
} else {
  // code to be executed if both conditions are false
}

Example:

void main() {
  int number = 0;

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

Ternary Operator

The ternary operator is a shorthand for if-else statements.

Syntax:

condition ? expression1 : expression2;

Example:

void main() {
  int number = 5;
  String result = number > 0 ? 'Positive' : 'Negative or Zero';
  print(result);
}

  1. Loops

for Loop

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

Syntax:

for (initialization; condition; increment/decrement) {
  // code to be executed
}

Example:

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.

Syntax:

while (condition) {
  // code to be executed
}

Example:

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 is executed at least once.

Syntax:

do {
  // code to be executed
} while (condition);

Example:

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

  1. Switch Statements

The switch statement allows you to execute one block of code among many based on the value of a variable.

Syntax:

switch (variable) {
  case value1:
    // code to be executed if variable == value1
    break;
  case value2:
    // code to be executed if variable == value2
    break;
  default:
    // code to be executed if variable doesn't match any case
}

Example:

void main() {
  String grade = 'A';

  switch (grade) {
    case 'A':
      print('Excellent');
      break;
    case 'B':
      print('Good');
      break;
    case 'C':
      print('Fair');
      break;
    case 'D':
      print('Poor');
      break;
    default:
      print('Invalid grade');
  }
}

Practical Exercises

Exercise 1: Even or Odd

Write a Dart program that checks if a number is even or odd.

Solution:

void main() {
  int number = 4;

  if (number % 2 == 0) {
    print('The number is even.');
  } else {
    print('The number is odd.');
  }
}

Exercise 2: Factorial Calculation

Write a Dart program to calculate the factorial of a number using a for loop.

Solution:

void main() {
  int number = 5;
  int factorial = 1;

  for (int i = 1; i <= number; i++) {
    factorial *= i;
  }

  print('The factorial of $number is $factorial.');
}

Exercise 3: Grade Evaluation

Write a Dart program that evaluates a student's grade using a switch statement.

Solution:

void main() {
  String grade = 'B';

  switch (grade) {
    case 'A':
      print('Excellent');
      break;
    case 'B':
      print('Good');
      break;
    case 'C':
      print('Fair');
      break;
    case 'D':
      print('Poor');
      break;
    default:
      print('Invalid grade');
  }
}

Common Mistakes and Tips

  • Forgetting the break statement in switch cases: This can lead to fall-through behavior, where multiple cases are executed.
  • Infinite loops: Ensure that the loop's condition will eventually become false to avoid infinite loops.
  • Using the ternary operator for complex conditions: The ternary operator is best used for simple conditions. For complex conditions, use if-else statements for better readability.

Conclusion

In this section, you learned about control flow statements in Dart, including conditional statements, loops, and switch statements. These constructs are essential for controlling the flow of your program and making decisions based on conditions. Practice the exercises provided to reinforce your understanding and prepare for more advanced topics.

© Copyright 2024. All rights reserved