Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 02:00 30 Apr 2024 Privacy Policy
Jump to

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 Zealand
Posts: 2289
Posted: 04:28am 17 Oct 2014
Copy link to clipboard 
Print this post

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: Australia
Posts: 5905
Posted: 10:25am 17 Oct 2014
Copy link to clipboard 
Print this post

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: Australia
Posts: 3165
Posted: 11:26pm 17 Oct 2014
Copy link to clipboard 
Print this post

  robert.rozee said  can i rely upon this interpretation - particularly of IF THEN - to remain unchanged in future releases?

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 Zealand
Posts: 2289
Posted: 03:00am 18 Oct 2014
Copy link to clipboard 
Print this post

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 :-)Edited by robert.rozee 2014-10-19
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 04:50am 18 Oct 2014
Copy link to clipboard 
Print this post

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: Australia
Posts: 5016
Posted: 01:41pm 18 Oct 2014
Copy link to clipboard 
Print this post

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.

GlennEdited by Gizmo 2014-10-20
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: Australia
Posts: 935
Posted: 02:00pm 18 Oct 2014
Copy link to clipboard 
Print this post

  Gizmo said   Those if/thens/elses spread over a single line with multi commands have always confused me. [/quote]

I have the same trouble.

[quote]But if there is more than one command, I avoid the colon and use multi lines instead....

if A=1 then
print "Yes"
B=1
else
print "No"
B=0
end if

It means more lines of code, but eliminates any confusion, so makes the code easy to follow.


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: Australia
Posts: 1094
Posted: 12:03am 19 Oct 2014
Copy link to clipboard 
Print this post

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 Zealand
Posts: 2289
Posted: 12:25am 19 Oct 2014
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 2794
Posted: 01:02am 19 Oct 2014
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 676
Posted: 06:30am 20 Oct 2014
Copy link to clipboard 
Print this post

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: Australia
Posts: 1328
Posted: 05:39pm 20 Oct 2014
Copy link to clipboard 
Print this post

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


To reply to this topic, you need to log in.

© JAQ Software 2024