Check if a score falls within the range 1 to 10.

← Operators and Expressions · Ref: Q1102

Range membership:

  valid <- score is in 1 ... 10

Inclusive on both ends. Works with any comparable type (Integer, Float, Character, Date). Bounds can be variables. See Q1101 for is in with collections, Q1099 for contains (substring).

Example

#!ek9
defines module qa.operators.isinrange

  defines program

    IsInRangeDemo()
      stdout <- Stdout()

      upperBound <- 10

      //Inside the range
      inside <- 5 is in 1 ... upperBound
      stdout.println(`5 in 1..10: ${inside}`)

      //Outside the range
      outside <- 15 is in 1 ... upperBound
      stdout.println(`15 in 1..10: ${outside}`)

      //On the boundary (inclusive)
      boundary <- 1 is in 1 ... upperBound
      stdout.println(`1 in 1..10: ${boundary}`)
Other ways to ask this
  • I need to validate that a number is between a minimum and maximum
  • In Python I'd use 'if 1 <= x <= 10'. Write the EK9 range check
  • Given a user input, verify it falls within an allowed range using 'is in'
  • Test whether an integer is within a start ... end range

Coming from another language?

Python: 1 <= x <= 10. Java: x >= 1 && x <= 10. Rust: (1..=10).contains(&x). Go: x >= 1 && x <= 10. EK9: x is in 1 ... 10.

Keywords: between, bounds, inclusive, is in, range, validate