Menu

How does this code work with nested IF's ?

rowdy
2015-07-11
2017-12-08
  • rowdy

    rowdy - 2015-07-11

    On this page
    http://gcbasic.sourceforge.net/index.php
    there is a link to some sample code
    http://gcbasic.sourceforge.net/examples.html
    on that page is an example of a morse code generator
    within that code is a bunch of nested IF's
    on doing some reading in the forum, nested IF's
    are not supposed to be supported , so how does the code work ?

     
  • Hugh Considine

    Hugh Considine - 2015-07-11

    Nested Ifs are definitely allowed! The morse code generator is quite an old example, I haven't tried it myself since 2006 but it should still work. If you try it and it doesn't work, or if you have any other questions please let us know!

     
  • rowdy

    rowdy - 2015-07-11

    Hi Hugh
    thanks for reply, so they are in fact supported.

    With regards to what was written on this forum about nested IF's , I was referring to the second post in this link, where the replier said nested IF's are not supported
    https://sourceforge.net/p/gcbasic/discussion/1073404/thread/52ad3f50/

     
  • Hugh Considine

    Hugh Considine - 2015-07-11

    I think the problem in that post was the way that the Ifs were nested. The code was like this:

    if volts > 230 then
        'stuff
        if volts > 150 and volts < 230 then
            'stuff that can never happen
            '(if volts is between 150 and 230, the first If will be skipped and so will this)
        end if
    end if
    

    But normally there isn't a problem with having nested Ifs, I quite often use them. Nested Ifs can be more efficient than using And in a condition if you need to check for two things.

     
  • rowdy

    rowdy - 2015-07-11

    Thank you I will give it a go, cheers.

     
  • George Alvarez

    George Alvarez - 2017-12-08

    standy by, eating words now....

    OK, there's a peculiar thing that makes it appear as if it doesn't work.

    If you start out like this:

             'previous if code
         else
            if current_state = 1 then
              stable_count = stable_count + 1
              ...
    

    GCB will not recognize the nested if until you put a carriage return/line feed after the else, like this:

             'previous if code
    
         else
            if current_state = 1 then
              stable_count = stable_count + 1
              ...
    

    the moment you do that, the code will recognize the nested if. Afterwards, you can remove the CRLF, and it still works.

    Strange.

     

    Last edit: George Alvarez 2017-12-08

Log in to post a comment.