Range on an integer counter [SOLVED]?

I want to print a message based on a range in a turnscript counter.

How would I do that?

if (depression.SquashCount = 1) {
  msg ("It looks like a slightly damp squash sitting in a small depression.")
}
else if (depression.SquashCount = 3) {
  msg ("It looks like a damp squash sitting in a small depression.  Is it me or does it smell a bit like vinegar in here?")
}
else if (depression.SquashCount = 5) {
  msg ("It is a soggy squash sitting in a tiny puddle of bacteria-laden water.  It definitely smells like a pair of your dad's sweaty old socks.")
}
else if (depression.SquashCount = 8) {
  msg ("To the untrained eye, it's hard to tell what this used to be, but you know it was a squash.  It's a mushy pile now and it smells of vinegar.")
}
else if (depression.SquashCount = 11) {
  msg ("A pasty pile of what used to be a squash.  Now, however, it has the consistency of applesauce and it reeks of vinegar.")
}

Above is the description I am going for with each range. I want one description to print from turns 1-2, second one from 3-4, etc. I want to print the same description from 11 to infinity.

Thanks in advance.

EDIT: Could I just do SquashCount = 1,2 and = 3,4 etc... and SquashCount >10?


Two ways to do it:

if (depression.SquashCount <= 2) {
  msg ("It looks like a slightly damp squash sitting in a small depression.")
}
else if (depression.SquashCount <= 4) {
  msg ("It looks like a damp squash sitting in a small depression.  Is it me or does it smell a bit like vinegar in here?")
}
else if (depression.SquashCount <= 7) {
  msg ("It is a soggy squash sitting in a tiny puddle of bacteria-laden water.  It definitely smells like a pair of your dad's sweaty old socks.")
}
else if (depression.SquashCount <= 10) {
  msg ("To the untrained eye, it's hard to tell what this used to be, but you know it was a squash.  It's a mushy pile now and it smells of vinegar.")
}
else {
  msg ("A pasty pile of what used to be a squash.  Now, however, it has the consistency of applesauce and it reeks of vinegar.")
}

Using less than or equal to means that you're covering a range of values; and the else if means that it'll exclude the values that were in the previous range, meaning that you only need one test per statement.

Or you could do:

switch (depression.SquashCount) {
  case (1,2) {
    msg ("It looks like a slightly damp squash sitting in a small depression.")
  }
  case (3,4) {
    msg ("It looks like a damp squash sitting in a small depression.  Is it me or does it smell a bit like vinegar in here?")
  }
  case (5,6,7) {
    msg ("It is a soggy squash sitting in a tiny puddle of bacteria-laden water.  It definitely smells like a pair of your dad's sweaty old socks.")
  }
  case (8,9,10) {
    msg ("To the untrained eye, it's hard to tell what this used to be, but you know it was a squash.  It's a mushy pile now and it smells of vinegar.")
  }
  default {
    msg ("A pasty pile of what used to be a squash.  Now, however, it has the consistency of applesauce and it reeks of vinegar.")
  }
}

which is more efficient if the ranges aren't always continuous, or if there's a large number of them and the most likely ones are in the middle; it can also be easier to read in some situations.


Awesome! Will give it a try. If you don't hear back from me, assume I am continuing down the road to completion!

Thanks again!


Log in to post a reply.

Support

Forums