Control Flow in Flutter: A Fun Guide to if, switch, for, and while

When building apps with Flutter (or Dart, to be precise), you'll quickly run into scenarios where your code needs to make decisions or repeat actions. That’s where control flow comes in! Just like traffic lights direct cars, control flow directs the flow of your program  deciding what runs, when, and how often.

What is Control Flow?

Control flow is simply the order in which code is executed. Dart, like most programming languages, gives us several tools to control that flow: if statements, switch cases, and looping tools like for and while.

1. if and else

The if statement is the most basic decision-maker. Think of it as: "If this condition is true, do something." You can also use else for an alternative path.


int age = 20;

if (age >= 18) {
  print("You're an adult!");
} else {
  print("You're still a minor.");
}

You can also chain multiple conditions using else if:


int score = 85;

if (score >= 90) {
  print("A");
} else if (score >= 80) {
  print("B");
} else {
  print("C or lower");
}

2. switch

Tired of writing a ton of if-else blocks? That’s where switch shines. It’s great when you have multiple specific values to check.


String day = "Monday";

switch (day) {
  case "Monday":
    print("Ugh, it's Monday!");
    break;
  case "Friday":
    print("Yay, it's Friday!");
    break;
  default:
    print("Just another day.");
}

Note: Don't forget to use break after each case unless you want a fall-through (which Dart doesn't allow by default).

3. for loop

Need to repeat something a known number of times? Use a for loop. It's perfect for iterating through lists or counting.


for (int i = 0; i < 5; i++) {
  print("Count: $i");
}

You can also loop through lists:


List fruits = ['apple', 'banana', 'cherry'];

for (var fruit in fruits) {
  print(fruit);
}

4. while and do-while

If you want to keep looping until a condition is false (but you don't know how many times that will take), use while or do-while.


// while
int i = 0;
while (i < 3) {
  print("Hello $i");
  i++;
}

// do-while (runs at least once)
int j = 0;
do {
  print("Number $j");
  j++;
} while (j < 3);

When to Use Which?

  • Use if/else when you’re checking conditions that are boolean (true/false).
  • Use switch when you have several possible fixed values to check.
  • Use for when you know how many times to repeat.
  • Use while when you want to loop as long as something is true.

Tips & Best Practices

  • Keep your conditions simple and readable.
  • Don’t nest too many if blocks—it becomes messy fast.
  • Use meaningful variable names to make logic easier to follow.
  • Always test edge cases (e.g., empty list, null value).

Wrapping It Up

Control flow might sound like a fancy term, but it’s really just how we make our code act smart. Whether it's choosing a path with if, selecting from options with switch, or repeating actions with for and while, you now have the power to guide your Flutter app’s behavior.


0 Comments:

Post a Comment