Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Sugarcube] If within number range

Is there something I can put in an "if" statement to test if something's within a certain number range? Something like:
<<if $Percent is 1-50>>Thing!<<endif>><<if $Percent is 50-100>>Other thing!!<<endif>>

I know that as an alternative I could do something like:
<<if $Percent is gt 1>><<if $Percent is lt 50>>Thing!<<endif>><<endif>><<if $Percent is gt 50>><<if $Percent is lt 100>>Other thing!<<endif>><<endif>>

But this produces much more clutter and is slightly more time-consuming, so I figured I'd ask if there was already a built-in "within these numbers" command for if statements that I didn't know about.

Thanks so much!

Comments

  • Well, I'd use the "and" operator.
    <<if $Percent gte 1 and $Percent lte 50>>Thing!<<elseif $Percent gt 50 and $Percent lte 100>>Other thing!!<</if>>
    

    You need to be careful, too, because in your second example, if $Percent is either 1, 50 or 100, you don't actually execute any of the ifs. Also, I'm pretty sure you should use either "is" or "gt", not both at the same time.

    I suggest you read this
  • edited February 2017
    If you want an even shorter statement you can think about how an 'if' statement evaluates in order from top to bottom. If it find something it can execute, it will do that and close out. Therefore, you can have your code be this instead and it will output the same thing.
    <<if $Percent gte 50>>Other thing!!<<else>>Thing!<<endif>>
    

    If the number is greater than or equal to 50, it will put out "Other Thing!!". Otherwise, it will output "Thing!". This is because if the first statement is true, it executes the contents and closes out the if statement. If it isn't, then it will move on and check the next thing.

    If you expect your values to always be between 1 and 100, this fulfills the same function with a lot less clutter.
  • I suggest you read this

    I read this before but somehow completely skipped over "and" statements without realizing it! Thanks so much, this is really helpful!
Sign In or Register to comment.