Hey guys, Can I do something like this with when e...
# announcements
a
Hey guys, Can I do something like this with when expresion?
Copy code
val arrayNumber = arrayOf(0, 1, 2, 3)

if (arrayNumber.contains(0)) {
    //DO SOMETHING FOR CASE 0
}

if (arrayNumber.contains(1)) {
    //DO SOMETHING FOR CASE 1
}

if (arrayNumber.contains(2)) {
    //DO SOMETHING FOR CASE 2
}

if (arrayNumber.contains(3)) {
    //DO SOMETHING FOR CASE 3
}
Thanks in advance!
s
I'd use the
in
keyword instead:
0 in arrayNumber
p
Hey, you could write something like this
Copy code
when {
  0 in arrayNumber -> DO X
  1 in arrayNumber -> DO Y
...
}
❤️ 5
a
It's inclusive when mets more than 1 case?
s
Also, the
when
and its clauses are like
else if
. Since your
arrayNumber
can have both a 0 and a 1 (and others), using a
when
will change the behavior of your code
❤️ 1
a
Okey that answer my question, Thanks you so much guys!! <3
d
e
0..3
Range
No. Need 1 by 1