Help simplifying a small if-script [Solved]

Hello! I was just curious if it were possible to shrink this script...

if (calendar.month=1) {
}
else if (calendar.month=2) {
}
else if (calendar.month=12) {
}
else {
}

into something like...

if (calendar.month=1,2,12) {
}
else {
}

or something like that? Any help would be greatly appreciated!

Anonynn.


Io

Not to my knowledge, but you could use a Switch case.

switch (calender.month){
case(1){
}
case(2){
}
case(3){
}
etc
}

IO... close...
switch (calender.,month){
case(1,2,12){
// do this
}
}
(I think, let me check.)
Yep, it works that way.


I was trying to avoid doing it via the switch way. But good to know for the future that it definitely can be done like that! Thank you guys so much :D

Anonynn.


Io

DarkLizerd, doesn't that just result in the cases 1, 2, and 12 all doing the same thing?

Presumably he wants them to do different things; it looks like he's doing a 'if calendar.month=1 set calendar.monthstring to "January" ' type deal.


Io,
Anonynn's example showed if calendar.month=1,2,12...
Also... If can check for multiple conditions on one line...

T=calendar.month
if (T=1 or T=2 or T=12) {
  msg ("Hi. T=" + T)
} 

Again, this will test T to see if it is 1, 2, or 12...
I like shorter variables... it makes typing so much easer... less chance to mistype a lone name.


Io

DarkLizerd, it'd perhaps be simpler if we knew precisely what it was he wanted to make. He may very well have assumed calender.onth=1,2,12 would do something other than what it actually does.


Maybe something was to happen only on those months...
OH!!! Winter months!!!
If month=winter
then, there is snow on the ground..
else,
no snow.


You could also do something like:

if (ListContains (Split ("1;2;12"), ""+month)) {

But that's even less efficient.

Really, this is exactly what switch is made for. I've not actually tested it in Quest, but in most languages switch is optimised heavily enough that there's a noticeable speed improvement for using it.


Log in to post a reply.

Support

Forums