Ternary operator - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +--- Thread: Ternary operator (/showthread.php?tid=2130) |
Ternary operator - Kernelpanic - 10-30-2023 Can one also integrate the declaration of a library into a function? The reason for the question is that I wrote a small program that allows one to use the tenary operator from C. If you could put the whole thing into a function, it might be useful for Basic. Maybe. The Basic-Program: Code: (Select All)
The C part: Code: (Select All) //TenaerenOperator von Basic aufrufen - 30. Okt. 2023 RE: Ternary operator - DSMan195276 - 10-30-2023 Could you clarify what you're trying to achieve? You don't need C to write that function, the ternary operator is the same as just using an `if` for your usage: Code: (Select All)
Unfortunately if you want a "real" ternary operator you can't implement that as a function. The ternary operator is only supposed to evaluate one of the true or false paths, not both, but there's no way to achieve that with a function call. RE: Ternary operator - bplus - 10-30-2023 Code: (Select All) Print Iff$(_Width > _Height, "Landscape View", "Portrait View") RE: Ternary operator - Kernelpanic - 10-30-2023 I know about "If", but in C, Java etc. the tenary operator is there not for fun. Again: Can something like this be implemented in a function in QB64? RE: Ternary operator - DSMan195276 - 10-31-2023 (10-30-2023, 11:50 PM)Kernelpanic Wrote: Again: Can something like this be implemented in a function in QB64?Not in the way it behaves in languages like C and Java, it would have to be built into the language to work properly. Ex. In C and Java you can do stuff like this: Code: (Select All) x = (a != 0)? 20 / a: 0; // Checks that a is not zero before dividing by it RE: Ternary operator - bplus - 10-31-2023 Code: (Select All) For a = -1 To 1 RE: Ternary operator - DSMan195276 - 10-31-2023 (10-31-2023, 01:37 AM)bplus Wrote:Try it again, but skip the floating point math (where division by zero is technically allowed). Do `20 \ a` and the program always explodes, since it gets evaluated before calling the `Iff$()` function. RE: Ternary operator - bplus - 10-31-2023 Got it! Thanks for the good example. RE: Ternary operator - Kernelpanic - 10-31-2023 (10-31-2023, 01:13 AM)DSMan195276 Wrote:Yes, that obviously does not work. How should such and similar variations be intercepted?(10-30-2023, 11:50 PM)Kernelpanic Wrote: Again: Can something like this be implemented in a function in QB64?Not in the way it behaves in languages like C and Java, it would have to be built into the language to work properly. Ex. In C and Java you can do stuff like this: Gets kind of complicated. Code: (Select All)
|