Ternary or if/else in BASIC

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Ternary or if/else in BASIC

Post 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

Code: Select all

V = F
IF (E) THEN V = T
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?
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
bjonte
Vic 20 Hobbyist
Posts: 110
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Ternary or if/else in BASIC

Post 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.

Code: Select all

V=F-E*(T-F)
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.

Code: Select all

V=(E+1)*F-E*T
Neither of these solutions are "neat" in that they are easy to read and understand. They obfuscate the code somewhat.
User avatar
huffelduff
Vic 20 Hobbyist
Posts: 118
Joined: Sat Sep 05, 2020 9:14 am

Re: Ternary or if/else in BASIC

Post 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
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Ternary or if/else in BASIC

Post 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.
User avatar
bjonte
Vic 20 Hobbyist
Posts: 110
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Ternary or if/else in BASIC

Post 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!
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Ternary or if/else in BASIC

Post 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.
User avatar
huffelduff
Vic 20 Hobbyist
Posts: 118
Joined: Sat Sep 05, 2020 9:14 am

Re: Ternary or if/else in BASIC

Post by huffelduff »

Mike wrote: Sun Jan 15, 2023 3:28 am "V = 30 - V"
Cool trick!
Post Reply