wahyu
wahyu

Reputation: 2405

How to do looping that contain if statement in dart language

I am trying to do looping in dart with 100 data, but I am confused how to do it inside if statement... what I mean is.. usually I am able doing this with some data

if(x[0]<5 || x[1]<5 || x[2]<5){
      //do something
  }

but... inside if statement, I want to define 100 data.. I want to do like this

if(x[0]<5 || x[1]<5 || x[2]<5 ||....|| x[99]<5){
      //do something
  }

is that possible to do that using for looping since I have || (or) condition

Upvotes: 2

Views: 859

Answers (2)

jamesdlin
jamesdlin

Reputation: 90125

As Doc answered, using any is appropriate in your case.

In general (and this applies for any programming languages, not just Dart), if you want a loop, create a loop.

for (var element in x) {
  if (element < 5) {
    // do something

    // Stop looping; equivalent to ||'s short-circuiting behavior.
    break;
  }
}

Upvotes: -1

Doc
Doc

Reputation: 11651

you can use any

void main() {
  var x = [1, 2, 3, 4, 5];
  if (x.any((v) => v < 5)) {        //true
    print("atleast one is less than 5");
  }

  var y = [1, 2, 3, 4, 5];
  if (y.any((v) => v > 50)) {        //false
    print("atleast one is more than 50");
  }
}

tested on dartpad

Upvotes: 4

Related Questions