DEV Community

Sudhanshu Kumar Yadav
Sudhanshu Kumar Yadav

Posted on

Loop Labels: The Unsung Heroes

Picture this: you're a maestro conducting an orchestra of nested loops. Each instrument has its own loop, its own rhythm, but sometimes, the tuba needs to take a pause. That's where the power of Dart's loop labelling enters stage right. It's the equivalent of a conductor's baton, directing which section needs to continue and which needs a breather.

label

Loop labels are like secret agents. They're often overlooked, but when you need them, they swoop in and save the day. And in Dart, they can be your best friend in the wild, wild west of nested loops.

Imagine you're in charge of a system for a school district. You need to search across multiple schools, each with multiple classes and students, to find a particular student based on their name. Suppose that a student could be temporarily marked as inactive in a class if they are absent. We will check the status of the student and act accordingly:

  1. If we find the student and they are enrolled and active, we'll break all loops as we're done with our search.

  2. If we find the student and they are enrolled but inactive, we'll break the current student loop and continue with the next student in the same class.

  3. If we find the student but they're not enrolled, we'll break the current class loop and continue with the next class or school.

void main() {
  var district = {
    'School A': {
      'Class 1': [{'name': 'Alice', 'enrolled': true, 'active': true}, {'name': 'Bob', 'enrolled': false, 'active': false}, {'name': 'Charlie', 'enrolled': true, 'active': true}],
      'Class 2': [{'name': 'Daniel', 'enrolled': true, 'active': true}, {'name': 'Eva', 'enrolled': true, 'active': true}, {'name': 'Frank', 'enrolled': false, 'active': true}],
    },
    'School B': {
      'Class 1': [{'name': 'Grace', 'enrolled': true, 'active': true}, {'name': 'Hannah', 'enrolled': true, 'active': true}, {'name': 'Ian', 'enrolled': true, 'active': true}],
      'Class 2': [{'name': 'Frank', 'enrolled': true, 'active': false}, {'name': 'Katie', 'enrolled': true, 'active': true}, {'name': 'Laura', 'enrolled': true, 'active': true}],
    },
    // Imagine there are many more schools and classes
  };

  var searchStudent = 'Frank';

  schoolLoop: for (var school in district.keys) {
    classLoop: for (var className in district[school].keys) {
      studentLoop: for (var student in district[school][className]) {
        if (student['name'] == searchStudent) {
          if (student['enrolled']) {
            if (student['active']) {
              print('Found $searchStudent active and enrolled in $className of $school!');
              break schoolLoop;
            } else {
              print('Found $searchStudent enrolled but inactive in $className of $school.');
              break studentLoop;
            }
          } else {
            print('Found $searchStudent not currently enrolled in $className of $school.');
            break classLoop;
          }
        }
      }
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

This is a neat trick that can save computing power and make your programs more efficient. In real-life scenarios, you might have more complex conditions, more nested loops, or other operations, but the principle remains the same. By judiciously using loop labels in Dart, you can write cleaner, more efficient, and more readable code.

Wrapping It Up

Labels, in essence, give you the power to break free from not just one loop, but a whole set of them. It's like having a golden ticket that lets you exit the amusement park whenever you want. No need to take the monorail all the way around just to reach the exit!

But remember, with great power comes great responsibility. Don't go breaking loops willy-nilly. Only do so when it's truly needed.

Top comments (0)