Page 1 of 1
Ternary or if/else in BASIC
Posted: Sat Jan 14, 2023 4:41 pm
by chysn
What do you think is the most elegant way to simulate a ternary operator in CBM BASIC V2?
Code: Select all
type variable = expression ? if_true : if_false;
a.k.a.:
type variable = 0;
{
...
if (expression) variable = if_true;
else variable = if_false;
}
I'm thinking something like
That might be it. But what if I want to toggle between a two state values on subsequent iterations, like this:
Code: Select all
variable = (variable == state_1) ? state_2 : state_1;
Could be
Code: Select all
VT = V:REM TEMPORARY
V = S2:REM ASSUME STATE 2
IF (VT = S2) THEN V = S1:REM SWITCH TO STATE 1
But that seems a little bit verbose as a flip-flopping ternary operation? Is there something better?
Re: Ternary or if/else in BASIC
Posted: Sun Jan 15, 2023 12:03 am
by bjonte
It was a long time ago I programmed BASIC V2 so take this with a grain of salt. And I didn’t test this so I may have made an mistake.
The comparison results in either -1 or 0 so this can be used mathematically to set values. It can be neat sometimes but is probably not the most efficient solution.
This sets the result to the false value and if the expression is true it adds the difference between true and false values so it becomes the true value.
The disadvantage with this solution is that the math may mess up the values and return with a small error if one of the numbers is close to zero and the other is far from zero. It should work well for integers. This can be fixed by repeating the expression twice.
Neither of these solutions are "neat" in that they are easy to read and understand. They obfuscate the code somewhat.
Re: Ternary or if/else in BASIC
Posted: Sun Jan 15, 2023 1:23 am
by huffelduff
Hi there Chysn and Bjonte,
This is what I got:
Toggling values example: y toggles between 20 and 30
Integer values only and its rather slow because of two logical evaluations and two multiply's:
Code: Select all
1 y=30
2 y=-20*(y=30)-30*(y=20)
3 printy:goto2
Else as line 2. The goto in line 1 is only processed if y=20
Code: Select all
1 if(y=30)theny=20:goto3
2 y=30
3 printy:goto1
Greetings
H
Re: Ternary or if/else in BASIC
Posted: Sun Jan 15, 2023 3:28 am
by Mike
Toggling between two numerical values is usually done with basic arithmetic, by subtracting either value from the sum of both, i.e. with the two values 10 and 20 you'd write "V = 30 - V".
Actually, when reading "ternary", I first thought about the
three-way comparison in FORTRAN (less than, equal, greater than). In BASIC, that can be accomplished with
ON 2+SGN(A-B) GOTO/GOSUB lt,eq,gt - with A, B being (sub-)expressions on either side of the nominal relational operator, and "lt", "eq", "gt" being line numbers of appropriate blocks or procedures.
Re: Ternary or if/else in BASIC
Posted: Sun Jan 15, 2023 5:48 am
by bjonte
Mike wrote: ↑Sun Jan 15, 2023 3:28 am
Toggling between two numerical values is usually done with basic arithmetic, by subtracting either value from the sum of both, i.e. with the two values 10 and 20 you'd write "V = 30 - V".
It can hardly get more simple than that. We have a winner!
Re: Ternary or if/else in BASIC
Posted: Sun Jan 15, 2023 8:07 am
by chysn
Mike wrote: ↑Sun Jan 15, 2023 3:28 am
Toggling between two numerical values is usually done with basic arithmetic, by subtracting either value from the sum of both, i.e. with the two values 10 and 20 you'd write "V = 30 - V
Yeah! That’s it. On iterations for BASIC sound effects, a calculation like that makes a ton of difference, since BASIC is so slow.
Re: Ternary or if/else in BASIC
Posted: Sun Jan 15, 2023 9:54 am
by huffelduff
Mike wrote: ↑Sun Jan 15, 2023 3:28 am
"V = 30 - V"
Cool trick!