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 : Exactly what does the colon ":" do?
Author | Message | ||||
Paul_L Guru Joined: 03/03/2016 Location: United StatesPosts: 769 |
What does the colon do? Is it the same as CR+LF? And, what will ENDIF do if it appears in a one line if structure? Will these four snippets produce the same result? if a=b then c=2 d=3 else c=4 d=5 endif if a=b then c=2:d=3 else c=4:d=5 if a=b then c=2:d=3 else c=4:d=5 endif if a=b then : c=2:d=3 : else : c=4:d=5 : endif Paul in NY |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 1986 |
sort of. generally the colon separates commands and statements just like CRLF might. Simply, it means "and do this" IF ... THEN ... ELSE ... on a single line comes from a more simple time before proper "structured" programming. Primitive basics used to only have this option (and some didn't even have ELSE) and so modern basics support this as legacy. The endif is implied by the end of the line. If the IF structure is split across many lines - which provides a nice structure to the statement, usually accompanied by indenting the lines to "nest" the code and make for easy reading, the endif has to be given explicitly so the interpreter knows where to close off the conditional code section. So in your examples above, only the first two will work properly; the first because it is nicely structured, the second because the endif is implicit. The last two will not work because endif has no place if the entire IF statement is on one line (because the ENDIF would only be "executed" if the IF clause is false) A colon on it's own does nothing because it just separates statements, so in your last example because a statement is expected after THEN (or ELSE) the colon will be ignored because the statement to the left of the colon is empty. You could just as easily have IF ... THEN ::::::: c=2 Some commands can't be used with colons (i.e. on a single line). SELECT ... CASE ... END springs to mind,but there are going to be loads if I really had a think about it. For IF, use whatever you feel is best... on one line with colons is more compact, broken over several line is more "modern" or readable, but watch out for gotchas. You cannot mix the two so; IF X=1 THEN y=2 z=3 ENDIF will fail (because it's is a mix of single line and structured). |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3022 |
Not so for the line where each statement is terminated with a colon: [code] If 1 = 0 Then : Print "C" : Print "D" : Else : Print "E" : Print "F" : EndIf Print "NEXT" If 1 = 1 Then : Print "C" : Print "D" : Else : Print "E" : Print "F" : EndIf > run E F NEXT C D [/code] SELECT CASE can also all be on one line with colons. [code] > list For i = 1 To 3 Select Case i : Case 1 : Print i*i : Case 2 : Print i*i : Case 3 : Print i*i : End Select Next i > run 1 4 9 [/code] PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 1986 |
wow! live and learn thanks |
||||
Paul_L Guru Joined: 03/03/2016 Location: United StatesPosts: 769 |
So ..... apparently ..... the ":" has the same meaning as <CR><LF> and the endif means nothing when it is on the end of a one line if .. then .. else. Very interesting! |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3675 |
There's a fair chance that's not quite right but your examples are so simple you can't tell for sure. You'd have to nest some within each other / other nested statements to see. (Or read the code!) BTW if : did as you say then x=SOMETHING y=0 if x=1 then y=1: y=2 print y would always print 2, but it doesn't (try SOMETHING as 1 and again as 0). John |
||||
Paul_L Guru Joined: 03/03/2016 Location: United StatesPosts: 769 |
@JohnS -- My statement was not complete. I should have said that: 1. The colon means the same thing as <CR><LF>. 2. An endif means nothing when it is on the end of a one line if ... else ... test. 3. A one line if ... else ... ends with the <CR><LF>. In this code x=something : y=0 if x=1 then y=1:y=2 print y When x=1 the test passes and y=1:y=2 executes so print y produces 2. When x<>1 the test fails and y=0 is not altered so print y produce 0. Paul in NY |
||||
Print this page |