Every "if" must have a matching "else" or a matching "endif"

mardi 26 juillet 2016

I've done the best I can to explain this the best way I can! I wouldn't call them lessons, more like observations and odd ends I figured out.
Like many other Mobius users I am NOT a programmer and I never wrote any code before I discovered Mobius, that was back in 2008 I think.
So I hope this helps someone… Above all else have fun!

Every "if" must have a matching "else" or a matching "endif".
Every "else" must have a matching "endif".
If you don't follow this rule the if statement will not execute the way you expect.
Code:

if something
  set something
endif

The "endif" is important if you have statements following it.
What this does is tell you where the end of the last else block is.
---------------------------------------------------------------------------------------------------------------



Long Chains of if/else/if
Because long chains of if/else/if statements are common you can eliminate the need for a long list of matching endifs at the end by putting "else" and "if" on the same line like this:
Code:

if something
  set something
else if something
  set something
else if something
  set something
else if something
  set something
endif

Again, "endif" tell you where the end of the last else block is.
---------------------------------------------------------------------------------------------------------------



"else" and "if" on different lines
"else" and "if" on different lines are subtly different than "else if" on one line.
When you use "else" and "if" on different lines, each "if" must be terminated with an "endif" like this:
Code:

if something
  set something
else
  if something
      set something
  else
      if something
        set something
      else
          if something
            set something
          endif
      endif
  endif
endif

Again, That last "endif" tell you where the end of the last else block is.
----------------------------------------------------------------------------------------------------------------



Use indentation
When you have a long chain of if/else/if/else... statements, it is easer to write them with indents to make the flow more obvious. It helps you see where the code blocks are in an if statement.
Code:

if output > 126
    set output 111
else if output > 110
    set output 101
else if ...
...
endif

Again, don't forget that “endif” :)
------------------------------------------------------------------------------------------------------------------


0 commentaires:

Enregistrer un commentaire

 

Lorem

Ipsum

Dolor