Multiple Level, Single Line IF Statements

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Multiple Level, Single Line IF Statements

Post by MattC »

Hi,

I'm trying to understand the syntax (if this situation is, in deed, possible) if a multi level IF statement put into a single line. (This question is borne out of pure curiosity. I have not really needed this as yet.) The multi line version goes something like this:

Code: Select all

      FOR I% = -1 TO 0
        FOR J% = -1 TO 0
          IF I% THEN
            IF J% THEN
              PRINT "I AND J"
            ELSE
              PRINT "I AND NOT J"
            ENDIF
          ELSE
            IF J% THEN
              PRINT "NOT I AND J"
            ELSE
              PRINT "NOT I AND NOT J"
            ENDIF
          ENDIF
        NEXT
      NEXT
But I cannot figure out an easy way to do it in a single line. The one that works is:

Code: Select all

      FOR I% = -1 TO 0
        FOR J% = -1 TO 0
          IF I% THEN IF J% THEN PRINT "I AND J" ELSE IF I% THEN PRINT "I AND NOT J" ELSE IF NOT J% THEN PRINT "NOT I AND NOT J" ELSE IF NOT I% THEN PRINT "NOT I AND J"
        NEXT
      NEXT
Is this the easiest way to do it? It seems awfully complicated for something that seems straightforward in the first example. (Perhaps that's why multiline IF statements are so useful.) Or am I just seeing complication where there is none?

Matt
DDRM

Re: Multiple Level, Single Line IF Statements

Post by DDRM »

Hi Matt,

Looks logical enough to me: even in a single line statement the nesting is just the same. In other words, you need to close each internal IF, and then go on with the next outer layer.

But I agree the multiline version is much easier to read!

Best wishes,

D