##Question
I want to setup an if else statement that checks the value of a variable.
##Details
However, something different is that, it should check the value between 2 numbers
##Example
val a=6
How do I set the statement up that it checks if x is 3<x<7 and print in a message?
(That’s not specific to Android so you probably don’t want that tag on the question.)
You can use boolean logic. The operators &&
, ||
, and !
are the basics.
if (3 < x && x < 7) { println("hola") }
That syntax is similar to many other languages.
Or you can use ranges .
if (x in 10..20) { ... }
Note that ranges are inclusive - that will be true if x
is 10 or 20. So with your example using <
instead of <=
, it might be simpler to use the and operator (&&
).
gidds
July 7, 2020, 6:35pm
3
You can get a range which excludes its end point, with the until
operator:
if (x in 4 until 7)
print("in range")
(The range still includes its start point, though.)