DEV Community

JoeStrout
JoeStrout

Posted on

Quick Tip: Checking if a Number is in Range

This is the first in what I hope to be a frequent series of "quick tip" posts, in which I highlight some simple but useful trick that you may not know about MiniScript or Mini Micro.

Today, it's that boolean operators in MiniScript can be chained — and this turns out to be a really useful way to check if a number is within some valid range.

Suppose you're asking the user for a number from 1 to 100. If they enter a number outside that range, you want to print an error and ask again. The naive approach looks like this:

while true
    num = input("Enter a number from 1 to 100: ").val
    if num >= 1 and num <= 100 then break
    print "Out of range; try again."
end while
Enter fullscreen mode Exit fullscreen mode

This works perfectly fine. But it's not quite as clear or concise as this (pay attention to the if line):

while true
    num = input("Enter a number from 1 to 100: ").val
    if 1 <= num <= 100 then break
    print "Out of range; try again."
end while
Enter fullscreen mode Exit fullscreen mode

You may have seen this sort of notation in school, something like a < x < b, which means "x is between a and b". Our code above does the same thing: the if statement says "if num is between 1 and 100 inclusive, then break out of the loop." (It's inclusive because we used <= rather than just <.)

It's a simple pattern, but a surprisingly common one. In the 100 BASIC games ported to MiniScript, we used this sort of pattern 136 times. Here are some examples I found:

if 0 < numPlayers < 5 then break

if 0 < num <= historicalBattles.len then break

if 1 <= shotType <= 4 then return

return 0 < x <= n and 0 < y <= n

if 1 <= x <= n and 1 <= y <= pile[x] then break

return 0 < xy[0] < 11 and 0 < xy[1] < 11 and self.shipAt(xy) == null

Especially when you're checking two variables (often X and Y coordinates, or indexing into some 2D array), as in the last few examples above, this concise range check makes your code considerably simpler and easier to grok.

Of course it works with strings, too. Use cases for this are a bit harder to come by, but you could certainly imagine a menu with options labeled A through M, and an input loop like:

while true
    choice = input("Enter selection (A-M): ").upper
    if "A" <= choice <= "M" then break
end while
Enter fullscreen mode Exit fullscreen mode

So, this is a handy trick whenever you need to check whether a variable is between two limits (inclusive or otherwise). Now that you know this exists, I bet you'll find uses for it surprisingly often. Happy coding!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay