Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conditional operator?
#1
Whenever I'm programming anything in ZX BASIC that involves non-trivial calculations, I really miss the conditional operator from C. So much that I finally decided to post a request here...

Since ZX BASIC already incorporates many useful operators from C like << (binary shift) and & (binary and), could you please consider incorporating C conditional operator also? What I mean is allowing code like this:

Code:
IF (a < 30) THEN
    LET a = a + 2
ELSE
    LET a = a + 1
END IF

To be written like this:

Code:
LET a = a + (a < 30 ? 2 : 1)

Those unfamiliar with C programming may not recognize it at first, but after you get used to it, the latter version is a lot easier to read. It's also easier for a compiler to optimize a single expression using conditional operators, instead of multiple IFs. Also this kind of feature would not introduce any problems or ambiguity in the parser.

For more complex expressions it makes a huge difference. Here's another example:

Code:
IF (row > 0) THEN
    LET pos = pos | (move & UP)
ELSE
    LET pos = pos | TOP
END IF
IF (row < 21) THEN
    LET pos = pos | (move & DOWN)
ELSE
    LET pos = pos | BOTTOM
END IF
IF (col > 0) THEN
    LET pos = pos | (move & LEFT)
ELSE
    LET pos = pos | LEFTMOST
END IF
IF (col < 31) THEN
    LET pos = pos | (move & RIGHT)
ELSE
    LET pos = pos | RIGHTMOST
END IF

Using conditional operators, all this code above would be simplified to this:

Code:
LET pos = pos | (row > 0 ? move & UP : TOP) |
                (row < 21 ? move & DOWN : BOTTOM) |
                (col > 0 ? move & LEFT : LEFTMOST) |
                (col < 31 ? move & RIGHT : RIGHTMOST)

Makes sense?
Reply
#2
Bump!

I'm missing this feature a lot. Is there any chance it will be implemented?
Reply
#3
(02-27-2021, 02:40 AM)einar Wrote: Bump!

I'm missing this feature a lot. Is there any chance it will be implemented?

Yes, It's also planned.
Indeed, I was thinking of implementing this other one (FreeBasic / VisualBasic compatible): IFF

IFF(cond, expr on True, expr on false). The only constraint is that both actions return the same type (both Strings, both numeric)
Reply
#4
IFF would be great, thanks!!!
Reply
#5
(09-16-2014, 08:57 PM)einar Wrote:
Code:
IF (a < 30) THEN
    LET a = a + 2
ELSE
    LET a = a + 1
END IF

In Sinclair BASIC you can use AND and OR expressions for that.

In this case you'd need this:
LET a = ((a+2) AND (a < 30)) + ((a+1) AND (a >= 30))

Another example to truncate a float number:
LET n = -23.4
LET t = INT n + (1 AND SGN n = -1)

Unfortunately this doesn't work with Zx Basic Sad
Reply
#6
Can you explain further, please?
What does not work? Are you compiling with —sinclair or —strict-bool options?
If not, try it and let me know.
Reply
#7
Quote:In Sinclair BASIC you can use AND and OR expressions for that.

Yes, but try implementing my second example using AND and OR. It's both illegible and inefficient.
Reply
#8
(10-30-2021, 05:57 PM)boriel Wrote: Can you explain further, please?
What does not work? Are you compiling with —sinclair or —strict-bool options?
If not, try it and let me know.

I thought Sinclair option just involved the ATTR, SCREEN$ and POINT features.

I discovered this compiler some days ago and to me it's superb.

However, I've tested the following program and results aren't as I expected (I compiled it with ZX Basic 1.16.0):

Code:
10 let n = 2
20 let n = (n+1) AND n<3
30 print n
100 let n = 20
200 let n = (n+1) AND n<3
300 print n

These are:
With Fuse emulator, a Spectrum +2 prints:
3
0

With Zx Basic and Sinclair option enabled, the Spectrum prints:
1
0

With Zx Basic and strict boolean option enabled, the Spectrum prints:
1
0

With Zx Basic and both Sinclair and strict boolean options enabled, the Spectrum prints:
1
0


Now I've realized that my python version is 3.6.9. Could this be the problem?
Reply
#9
More precisely, Sinclair BASIC evaluates "x AND y" as follows:

Code:
x AND y results x, when y is not zero
X AND y results 0, when y is zero

And Boriel ZX Basic evaluates "x AND y" as follows:

Code:
x AND y results 1, when both x and y are not zero
X AND y results 0, when either x or y is zero

You only notice the difference when x is neither 1 or 0.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)