Quick Tip: Checking if a Number is in Range

joestrout

JoeStrout

Posted on November 8, 2023

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!

💖 💪 🙅 🚩
joestrout
JoeStrout

Posted on November 8, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

if locals == globals
miniscript if locals == globals

July 24, 2024

Quick Tip: Counting up to a limit
miniscript Quick Tip: Counting up to a limit

December 6, 2023