Yavuz Tarhan
Yavuz Tarhan

Reputation: 576

Dart While Loop usage

i have a logic mistake but i cant figure out, loop not stops when num= returnitemcount ?

void main() {
  List<int> listAll = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  List<int> exceptions = [2, 3, 4];
  List<int> exceptedlist(
      int returnitemcount, List<int> exceptions, List<int> listfrom) {
    List<int> returnlist = [];
    int num = 0;
    while (num < returnitemcount) {
      for (var i = 0; i < listfrom.length; i++) {
        if (!exceptions.contains(listfrom[i])) {
          returnlist.add(listfrom[i]);
          num++;
        }
      }
    }
    return returnlist;
  }

  List<int> mylist = exceptedlist(3, exceptions, listAll);
  print(mylist);
  // expected => 1,5,6
  // result => 1, 5, 6, 7, 8, 9
}

Upvotes: 0

Views: 210

Answers (1)

julemand101
julemand101

Reputation: 31269

Your problem is that your while are only evaluated twice. The first time num is 0 and the second time it is 6. It is not evaluated every time num is updated.

while loops does not mean that the code inside the block are running as long some statement are true. It means the code will loop as long the statement are true.

So what you need to do is to check num every time you update the value. Something like this:

void main() {
  List<int> listAll = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  List<int> exceptions = [2, 3, 4];
  List<int> exceptedlist(
      int returnitemcount, List<int> exceptions, List<int> listfrom) {
    List<int> returnlist = [];
    int num = 0;
    while (num < returnitemcount) {
      for (var i = 0; i < listfrom.length; i++) {
        if (!exceptions.contains(listfrom[i])) {
          returnlist.add(listfrom[i]);
          if (++num >= returnitemcount) {
            break;
          }
        }
      }
    }
    return returnlist;
  }

  List<int> mylist = exceptedlist(3, exceptions, listAll);
  print(mylist); // [1, 5, 6]
}

I am not sure what you code are going to do exactly but you should be able to skip the while loop:

void main() {
  List<int> listAll = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  List<int> exceptions = [2, 3, 4];
  List<int> exceptedlist(
      int returnitemcount, List<int> exceptions, List<int> listfrom) {
    List<int> returnlist = [];
    for (var i = 0; i < listfrom.length; i++) {
      if (!exceptions.contains(listfrom[i])) {
        returnlist.add(listfrom[i]);
        if (returnlist.length >= returnitemcount) {
          break;
        }
      }
    }
    return returnlist;
  }

  List<int> mylist = exceptedlist(3, exceptions, listAll);
  print(mylist); // [1, 5, 6]
}

And can properly even be shorten into:

void main() {
  final listAll = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  final exceptions = [2, 3, 4];

  final mylist = exceptedlist(3, exceptions, listAll);
  print(mylist); // [1, 5, 6]
}

List<int> exceptedlist(
        int returnitemcount, List<int> exceptions, List<int> listfrom) =>
    listfrom
        .where((element) => !exceptions.contains(element))
        .take(returnitemcount)
        .toList();

Upvotes: 1

Related Questions