I think I've got some funny expectations... I want to iterate the numbers from 1 to 10. As a while loop it goes like this:
def countMe = 1 while (countMe<11) { println countMe countMe++ }
I was expecting that the following would do this also:
[1..10].each { println it }
But it actually prints the IntRange
, not each Integer
in the range. What is the (syntactically) closest way to my [x..y].each{}
fantasy to get each of a list of numbers?
ValueRange. of(minValue, maxValue); range. isValidIntValue(x); it returns true if minValue <= x <= MaxValue - i.e. within the range.
You can also use Kotlin range m..n in a conditional statement like if-statement. Using in keyword, you can check if a value is in the given range.
Use parentheses not brackets:
(1..10).each{println it}
[1..10]
is a list of length 1 containing a single range.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With