Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 14:03 10 May 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 : Exactly what does the colon ":" do?

Author Message
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 11:54am 17 Mar 2017
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 1986
Posted: 01:35pm 17 Mar 2017
Copy link to clipboard 
Print this post

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).
Edited by CaptainBoing 2017-03-18
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3022
Posted: 01:57pm 17 Mar 2017
Copy link to clipboard 
Print this post

  CaptainBoing said  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)

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]
Edited by lizby 2017-03-19
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1986
Posted: 10:18pm 17 Mar 2017
Copy link to clipboard 
Print this post

wow! live and learn

thanks
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 11:37pm 17 Mar 2017
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 3675
Posted: 12:34am 18 Mar 2017
Copy link to clipboard 
Print this post

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).

JohnEdited by JohnS 2017-03-19
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 01:31pm 19 Mar 2017
Copy link to clipboard 
Print this post

@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


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

© JAQ Software 2024