If Statement
An if statement tests a condition to see if it is false(0) or true(non-zero) and
then executes the appropriate series of statements. There may be optional elsif and else clauses. e.g.
Since 1.0.2 curly braces can also be used for simple things / one-liners:
In other words the compiler says when it was expecting "end for" but found "end while", however no compiler could ever say it found "}" when expecting "}", because they are the exact same indistinguishable thing! I would certainly miss not being told that what I think is an "end if" is quietly being treated as an "end while", which is what C does to me twice a day.
if a<b then x = 1 end if if a=9 and find(0,s) then x = 4 y = 5 else z = 8 end if if char='a' then x = 1 elsif char='b' or char='B' then x = 2 elsif char='c' then x = 3 else x = -1 end ifNotice that elsif is a contraction of else if, but it is cleaner because it does not require an end if to go with it. There is just one end if for the entire if statement, even when there are many elsifs contained in it. However, JavaScript uses the longhand form, hence this is coloured illegal in .js files as a reminder to use "else if" instead.
Since 1.0.2 curly braces can also be used for simple things / one-liners:
if a<b { exit }It was always the case that a single test/action suffered the most from having to use "then .. end if". Note that elsif/else cannot be used within {}, and (quite deliberately) there is no equivalent shorthand end for/while/switch/function/procedure/type/try/etc. While the latter is more typing than "}", meaningful compiler error messages easily make up for that.
In other words the compiler says when it was expecting "end for" but found "end while", however no compiler could ever say it found "}" when expecting "}", because they are the exact same indistinguishable thing! I would certainly miss not being told that what I think is an "end if" is quietly being treated as an "end while", which is what C does to me twice a day.
- True story: I once dropped a single "}" sometime during a marathon refactoring (ten days with no compiling until every last class updated) of a large C++ program - the compiler then threw me a meaningless error in a completely unrelated file, and it took another three whole days just to track that compilation error down. There is gibberish, utter gibberish, and then whatever it was that compiler spat out at me. Obviously it got really, really, really confused by mixing "}" up, and I shudder to think what horrors it could potentially have unleashed had it somehow managed to complete the compilation quietly. Having half-decent syntax colouring in the code editor of the IDE I was forced to use might well have helped, but the real shocker for me was finding out that the £3,500 C++ compiler does not even count/check that brackets balance properly in each source file.