Page 1 of 1
IF statement on single line
Posted: Wed Mar 23, 2016 9:29 am
by gtonkin
@admins - please move to Tips and Tricks as I cannot post there directly.
This one is mildly interesting but thought some folks may find it saving them a few keystrokes here and there.
Instead of writing the following in
TI:
Code: Select all
IF(SUBST(vDim, 1, 1)@='}');
ITEMSKIP;
ENDIF;
You can simply write:
Code: Select all
IF(SUBST(vDim, 1, 1)@='}', ITEMSKIP, 0);
The "0" seems to be continue i.e. do nothing for the ELSE/FALSE part of the IF statement.
Not finding any documentation on this in the Dev or TI guide - IF is probably not the best keyword to use either.
Any comments on this or other little/unknown tips to save fingertips?
Re: IF statement on single line
Posted: Wed Mar 23, 2016 10:27 am
by Alan Kirk
gtonkin wrote:@admins - please move to Tips and Tricks as I cannot post there directly.
This one is mildly interesting but thought some folks may find it saving them a few keystrokes here and there.
Instead of writing the following in
TI:
Code: Select all
IF(SUBST(vDim, 1, 1)@='}');
ITEMSKIP;
ENDIF;
You can simply write:
Code: Select all
IF(SUBST(vDim, 1, 1)@='}', ITEMSKIP, 0);
The "0" seems to be continue i.e. do nothing for the ELSE/FALSE part of the IF statement.
Not finding any documentation on this in the Dev or TI guide - IF is probably not the best keyword to use either.
Any comments on this or other little/unknown tips to save fingertips?
Essentially the difference is that the block version is the TI implementation of If, and the single line is the Rules implementation of If which, being a Rules function, is of course available to TI as well. You're correct in your assessment of what it does and how.
It's not one of the more esoteric pieces of coding knowledge, but it's probably true that a lot of people "know" it (in the sense that they know you can use most Rules functions in TI) but don't know that they know it (in the sense that they've never really bothered to try to apply the Rules If() function when TI has its own perfectly serviceable syntax.)
I saw the Rules version used in some early code (early 2000's) that I inherited (and indeed it was often used for ItemSkip), and I probably used it myself a few times back then, but I moved away from it toward the block command. Not because I think there's anything actually
wrong with using the Rules If() function, simply because I find the block If makes the code more obvious and self-documenting. Also there's the fact that the Rules If essentially only works if you need to execute a single command like Itemskip; for any multi-line blocks you would have to use the Rules If block anyway which, IMHO would make the coding style more inconsistent and harder for anyone who comes after you to read.
If the True condition is a single line I think this one is down to a question of personal taste. My taste runs to not using it, but that's not to say that I think it's "wrong" to do so.
Re: IF statement on single line
Posted: Wed Mar 23, 2016 10:42 am
by pandinus
I frequently use this, as in the following:
Code: Select all
vVariable = IF(condition, vTrueVariable, vFalseVariable);
This makes for easy variable setting, requiring less lines of code.
Re: IF statement on single line
Posted: Wed Mar 23, 2016 10:56 am
by Alan Kirk
pandinus wrote:I frequently use this, as in the following:
Code: Select all
vVariable = IF(condition, vTrueVariable, vFalseVariable);
This makes for easy variable setting, requiring less lines of code.
That's a slightly different situation though. It's evaluating a value rather than executing flow control. Evaluating a value is arguably a Rules "thing" so it's not unreasonable to use the Rules function for that.
It's just mixing and matching flow control syntax that I'm not a fan of. But hey, Iboglix helps us with that by providing so few of them. For loops? Select case? Aaah, who needs 'em?
Re: IF statement on single line
Posted: Wed Mar 23, 2016 1:11 pm
by Wim Gielis
Re: IF statement on single line
Posted: Wed Mar 23, 2016 4:18 pm
by Steve Rowe
Pretty sure that back in the day we only had the rules version of the IF statement, the block If was a later addition.
Re: IF statement on single line
Posted: Wed Mar 23, 2016 4:52 pm
by tomok
Steve Rowe wrote:Pretty sure that back in the day we only had the rules version of the IF statement, the block If was a later addition.
Maybe because "back in the day", TurboIntegrator didn't even exist. You had to use processing worksheets.

Re: IF statement on single line
Posted: Wed Mar 23, 2016 6:17 pm
by Alan Kirk
Steve Rowe wrote:Pretty sure that back in the day we only had the rules version of the IF statement, the block If was a later addition.
It would have to have been in a very, very early model. I remember as far back as Tomok with map sheets in version 6. TI came in with version 7, though I can't recall whether it was there from 7.0. The earliest TI manual I have was for 7.1.3 (before then I think TM1 manuals were all in paper (

!!!) format) and we see even back then...

- TI_07_01_03_Extract.jpg (178.98 KiB) Viewed 8632 times
Re: IF statement on single line
Posted: Thu Mar 24, 2016 9:45 am
by Steve Rowe
Pretty sure I can remember the very first version of TI not having the block IF, as I can remember life becoming much easier process control wise when it came out.
Maybe they just forgot to document it...how times change!
Cheers