Shop Learn
Is it possible to add if-then-else syntax? — Parallax Forums

Is it possible to add if-then-else syntax?

We have now 2 forms of if available:

if a then b

or

if a then
 b
else
 c
endif

A simplified form

if a then b else c

can be also useful to make the code shorter and easier to read and write when what has to be done for if and else is a simple task - of course if possible and not too complex to implement.

An example of proposed syntax:

if header=1 then bpm=100 else bpm=200

Comments

  • ersmithersmith Posts: 5,287

    I tried doing the simplified if/then/else in an earlier compiler version and it did not work at all, so I gave up. I'll take another look at it. You're right, it would be a useful addition, but it's very tricky to specify the syntax in YACC because of all of the if/then forms that exist already :(.

  • MicksterMickster Posts: 2,051
    edited 2022-04-23 08:20

    bpm = 200: if header = 1 then bpm = 100

    Is this kosher? Not able to test, atm.

    Craig

    Edit: Just checked the manual and all indications are "yes" and even "then" can be omitted.

  • pik33pik33 Posts: 1,512

    @Mickster said:
    bpm = 200: if header = 1 then bpm = 100

    Is this kosher? Not able to test, atm.

    Craig

    Edit: Just checked the manual and all indications are "yes" and even "then" can be omitted.

    I recompiled a new flexprop and It seems the new syntax is already implemented.

    This:

    bpm = 200: if header = 1 then bpm = 100

    is a good old 8-bit Basic trick and it of course works, but if the variable is used by another cog and changes something there, the result can be something not expected. For the audio it can introduce clicks, for the robot the effects can be more unpleasant (tested...)

  • MicksterMickster Posts: 2,051

    @pik33 said:

    @Mickster said:
    bpm = 200: if header = 1 then bpm = 100

    Is this kosher? Not able to test, atm.

    Craig

    Edit: Just checked the manual and all indications are "yes" and even "then" can be omitted.

    I recompiled a new flexprop and It seems the new syntax is already implemented.

    This:

    bpm = 200: if header = 1 then bpm = 100

    is a good old 8-bit Basic trick and it of course works, but if the variable is used by another cog and changes something there, the result can be something not expected. For the audio it can introduce clicks, for the robot the effects can be more unpleasant (tested...)

    Ah, I always use a temporary variable in those cases and only assign the outcome of any decision :+1:

    Craig

  • ersmithersmith Posts: 5,287

    @pik33 said:
    I recompiled a new flexprop and It seems the new syntax is already implemented.

    >

    Yes, although one has to be careful not to nest the if/then/else on one line: more than one else on a line may not work as expected.

    I've also implemented the FreeBasic KILL command to delete files and directories.

  • pik33pik33 Posts: 1,512

    The full multi-line syntax is much better for complex if-elseif-else things. The simple if-then-else is for things that are simple. Nested one line ifs may be not readable at all :)

    I will use "kill" in the player - now I have to remove the card from P2 and use a PC to delete files that are broken or contain the music I don't like. After that, pressing del + confirming will delete the file without removing the card.

Sign In or Register to comment.