Conditional operator? einar Member Posts: 90 Threads: 21 Joined: Apr 2012 Reputation: 2 09-16-2014, 08:57 PM 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? einar Member Posts: 90 Threads: 21 Joined: Apr 2012 Reputation: 2 02-27-2021, 02:40 AM Bump! I'm missing this feature a lot. Is there any chance it will be implemented? boriel Administrator Posts: 1,757 Threads: 55 Joined: Aug 2019 Reputation: 24 02-28-2021, 05:29 PM (This post was last modified: 02-28-2021, 05:29 PM by boriel.) (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) einar Member Posts: 90 Threads: 21 Joined: Apr 2012 Reputation: 2 02-28-2021, 06:31 PM (This post was last modified: 02-28-2021, 06:31 PM by einar.) IFF would be great, thanks!!! javier2112 Junior Member Posts: 2 Threads: 0 Joined: Oct 2021 Reputation: 0 10-30-2021, 03:20 PM (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 boriel Administrator Posts: 1,757 Threads: 55 Joined: Aug 2019 Reputation: 24 10-30-2021, 05:57 PM 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. einar Member Posts: 90 Threads: 21 Joined: Apr 2012 Reputation: 2 10-30-2021, 06:33 PM (This post was last modified: 10-30-2021, 06:34 PM by einar.) 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. javier2112 Junior Member Posts: 2 Threads: 0 Joined: Oct 2021 Reputation: 0 11-01-2021, 04:14 PM (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? einar Member Posts: 90 Threads: 21 Joined: Apr 2012 Reputation: 2 11-01-2021, 09:12 PM 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. « Next Oldest | Next Newest »