Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : the syntax of IF
Author | Message | ||||
robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2289 |
i've always been a little troubled by the explanation of the single-line IF statement in the micromite manual. in part this is because of the way the lines of text are wrapped, creating the impression (at a casual glance) that IF THEN ELSE splits over two lines: IF expr THEN statement ELSE statement i know that this is just the way the manual is formatted and that the text explicitly says "This type of IF statement is all on one line", but the reality is that real programmers only quickly browse manuals. however, this aside aside, the explanation in the micromite manual, i feel, still does not capture the full behaviour. see the following short example: start:
Input a If a = 0 Then Print "ze"; : Print "ro" : GoTo start If a = 1 Then Print "one"; Else Print "<>1"; : Print "!" Print "next line" GoTo start > RUN ? 0 zero ? 1 one! next line ? 2 <>1! next line ? the first IF statement executes multiple statements if the conditional is true, up until the end of the line, and none of the statements if it is false - hence the output of "zero" and the loop back to start. the second IF statement executes only one statement per branch, with remaining statements on the line always executed (irrespective of conditional). hence the exclamation mark at the end of both "one!" and "<>1!". multiple statements between THEN and ELSE produce a runtime error if the conditional is true, and, surprisingly, no statements in the line execute if the conditional is false (i'd have liked to have seen a runtime error in both cases). so, as i see it, the syntax for single-line IF THEN and IF THEN ELSE should be expressed in the micromite manual as something along the lines of: IF expr THEN <statements> (where one or more statements are only executed if expr is true) and IF expr THEN statement_1 ELSE statement_2 [: <statements>] (where statement_2 is is executed when expr is false, and subsequent statements on the line are executed always) geoff, can i rely upon this interpretation - particularly of IF THEN - to remain unchanged in future releases? cheers, rob :-) |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5905 |
If only to prevent confusion, I would use the multi-line form of IF when ELSE is used. Jim VK7JH MMedit MMBasic Help |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3165 |
This was the way that Microsoft BASIC acted so I replicated its behaviour. And, yes, I do not have any plans to change it. Geoff Geoff Graham - http://geoffg.net |
||||
robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2289 |
thanks geoff, i was hoping that would be the answer. might i suggest that in the next revision of the micromite manual the formatting just for single-line IF THEN [ELSE] be changed to span the full width of the page so that no wrapping occurs? over the months i have also spotted quite a few small formatting errors in the document as a whole. none are major - the odd missing space and the likes. are you still interested in hearing about these? i've hesitated as they hardly warrant a forum posting or email. cheers, rob :-) |
||||
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2794 |
Rob I would definitely raise any 'formatting' errors with Geoff. I don't think that he will be upset being told about any of them as long as they are all valid. Remember that Geoff has created a lot of documented material; not to mention all his hardware creations too; so to 'filter' out these errors is not easy for one person to do by themselves. In my eyes there are three types of document 'issue': 1> A definite error (i.e. stating an incorrect pin number) 2> A recommended improvement to make something clearer (i.e. like your IF, THEN ,ELSE) 3> A personal suggestion to include something (i.e. like adding a switch debounce circuit) So in the interest of 'continual improvement' go ahead and drop him a PM and state which one of the above each point falls under. This way, Geoff can make quick changes as he feels fit without having to spend too much time interpreting peoples 'requests'. WW PS: @GeoffG; I hope you don't mind me voicing the above For everything Micromite visit micromite.org Direct Email: whitewizzard@micromite.o |
||||
Gizmo Admin Group Joined: 05/06/2004 Location: AustraliaPosts: 5016 |
Those if/thens/elses spread over a single line with multi commands have always confused me. Depending on how you read them, they can give different results, and the interpreter can have its own version of how it should work. The easy solution is clean formating. The way I do it is like this. If its a single command after the if conditions, I may keep it on one line to save space.... [code] if A=1 then print "Yes" else print "No" [/code] But if there is more than one command, I avoid the colon and use multi lines instead.... [code] if A=1 then print "Yes" B=1 else print "No" B=0 end if [/code] It means more lines of code, but eliminates any confusion, so makes the code easy to follow. Glenn The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
I have never liked putting multiple commands of any sort, separated by a colon, on one line. It may execute a bit quicker but I'll have slow every time if it means ease of reading when you go back to it in several years time. Bob |
||||
panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1094 |
I agree 100% with Bob. It' s even better (easier to read/follow) when indented with the format command in Jim's MMEdit. Doug. ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2289 |
perhaps folks are missing the point a little - my initial enquiry related to how a syntactically correct line was interpreted. geoff provided a clear and simple answer to that enquiry. quite aside from this, there are certainly varied opinions on programming style, preferred structure, and indenting. but these all come down to personal taste, with no right or wrong way. computers deal only in syntax, while humans deal mostly in semantics. cheers, rob :-) |
||||
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2794 |
Totally agree with Rob's last post And just to re-iterate; I am all for documentation 'improvements' if it helps to clarify things. For the absolute beginner - this can save hours of frustration WW For everything Micromite visit micromite.org Direct Email: whitewizzard@micromite.o |
||||
G8JCF Guru Joined: 15/05/2014 Location: United KingdomPosts: 676 |
My 2 pence worth, I tend to use IF Condition THEN Statement when there is only one SINGLE statement to execute if the Condition is true. When there is an else then I ALWAYS do multiline IF Condition THEN Statement(s) ELSE Statement(s) EndIf Whilst one can write IF THEN ELSE in a variety of ways, the optimal way is the one which most humans will understand/interpret correctly most quickly. I always write code on the basis of someone else having to pick it up in 5 years time and trying to figure out what it's supposed to do, ie maintainability. There are constructs in all languages (programming and otherwise) which whilst valid, and very compact, (and to some people very elegant), are always more difficult to interpret correctly (by humans) without a lot of scrutiny/thought. Peter The only Konstant is Change |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1328 |
Yes Peter, I tend to do it that way these days too, mainly because I've confused myself often enough in the past - and having MMEdit structure it nicely is a bonus. Greg |
||||
Print this page |