Colon Philosophy

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Colon Philosophy

#1 Post by shadeclan » 13 Oct 2011 13:08

No, I'm not talking about the one connected to your anus, moron! :roll:

In goto statements, it seems that colons are almost always optional. For example:

Code: Select all

goto:SomeLabel

... seems just as valid as ...

Code: Select all

goto SomeLabel

Only in cases of the implicit label EOF is it required:

Code: Select all

goto:EOF

My question is this - is there some advantage or disadvantage in colon usage? Has anyone found cases where a colon was or was not required in a goto statement?

See Ed? I still come here! :lol:

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Colon Philosophy

#2 Post by nitt » 13 Oct 2011 14:07

shadeclan wrote:No, I'm not talking about the one connected to your anus, moron! :roll:


That wasn't that funny...

And you never need the colon with tags.


Assembly

Code: Select all

org 100h
tag:
mov ah, 09h
mov dx, msg
int 21
jmp tag

C++

Code: Select all

#include <iostream>
int main() {
tag:
std::cout << "hi";
goto tag;
}

Batch

Code: Select all

@echo off
:tag
echo hi
goto tag
Last edited by nitt on 14 Oct 2011 17:05, edited 3 times in total.

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: Colon Philosophy

#3 Post by Rileyh » 13 Oct 2011 22:34

Nitt,
There is no point in explaining the question in assembly.
org 100h
tag:
mov ah, 09h
mov dx, msg
int 21
jmp tag


And in most cases, the colon is not needed to goto a tag.

Code: Select all

goto a
(is the same as) goto :a


But it does not affect the capability of the code. I just use the colon out of habit. :D

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Colon Philosophy

#4 Post by !k » 14 Oct 2011 00:18

(is the same as) goto :a
until you do not use an editor with code coloring
Image
;)

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Colon Philosophy

#5 Post by shadeclan » 14 Oct 2011 08:35

nitt wrote:
shadeclan wrote:No, I'm not talking about the one connected to your anus, moron! :roll:

That wasn't that funny...

Forgive the potty humor - I was trying to cut folks off "at the pass" as we say here in the US.


!k wrote:until you do not use an editor with code coloring

I, myself, use PSPad for batch editing. It does a pretty good job of highlighting except in two instances: indented labels, such as:

Code: Select all

:NormalLbl (normal highlighting)
...Code...
   :IndentedLbl (no highlighting)
   ...More code...

and internal function calls, such as:

Code: Select all

call:SomeFun (Label name is not highlighted)

It handles goto statements both with and without colons. By the way Ed, it actually supports a Dutch version!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Colon Philosophy

#6 Post by Ed Dyreen » 14 Oct 2011 11:32

shadeclan wrote:...
My question is this - is there some advantage or disadvantage in colon usage? Has anyone found cases where a colon was or was not required in a goto statement?

See Ed? I still come here! :lol:
The colon is no longer required since XP. It's still used in batches that need to be compatible !
Or was it the other way around ? Damnet now I am confused myself, there is a difference, otherwise I wouldn't have written code like:

Code: Select all

@rem :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@rem:: Suppress all 'direct call' related errors, all Windows OSes.
@rem :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@rem::(
@   if "%$ScriptHost.FullPathFile%"=="" 2>nul goto :ENDOLDOS ()
@   if "%$ScriptHost.FullPathFile%"=="" 2>nul goto  ENDOLDOS ()
@rem::)
@rem :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Code: Select all

:ENDOLDOS ()
@rem ::
@rem :: Exit 'old' OS
@rem ::
@rem ::(
@rem    :: Initialize 'old' OS
@rem    ::(
@       echo Off
@rem    ::)

@rem    :: Print reason
@rem    ::(
@       echo.
@       echo. Direct call to SubRoutine not allowed ^!
@rem    ::)

@rem    :: Pause until user confirms
@rem    ::(
@      2>nul set "?= " <nul
@rem      ::
@      pause
@rem    ::)

@rem    :: CLearScreen may be needed to force close on exiting an 'old' OS
@rem    ::(
@      cls
@rem    ::)

@rem    :: OS may not support the 'exit error' command
@rem    ::(
@      2>nul exit 1
@rem    ::)

@rem    :: OS should support the 'goto EOF' command
@rem    ::(
@      goto EOF
@rem    ::)
@rem ::
goto EOF ()
@rem ::)

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Colon Philosophy

#7 Post by shadeclan » 14 Oct 2011 11:38

I was wondering when you'd show up!


Ed Dyreen wrote:now I am confused myself, there was a difference ...

Well, think about it and let me know.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Colon Philosophy

#8 Post by nitt » 14 Oct 2011 17:02

Rileyh wrote:Nitt,
There is no point in explaining the question in assembly.


-_- Is Assembly really the only one you noticed? Did you miss the C++ and Batch example?

My point was exactly what I said before it, that you never need to use the colon when "going to" a tag.

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Colon Philosophy

#9 Post by shadeclan » 17 Oct 2011 06:29

nitt wrote:-_- Is Assembly really the only one you noticed? Did you miss the C++ and Batch example?

Actually, I noticed, although I am ignorant of any C language :( and don't know assembly :cry:.

Assembly has always fascinated me but I have never had the time to learn.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Colon Philosophy

#10 Post by nitt » 17 Oct 2011 13:18

shadeclan wrote:
nitt wrote:-_- Is Assembly really the only one you noticed? Did you miss the C++ and Batch example?

Actually, I noticed, although I am ignorant of any C language :( and don't know assembly :cry:.

Assembly has always fascinated me but I have never had the time to learn.


I don't know Assembly, either. But I know a lot of it.

http://www.youtube.com/watch?v=mWeh3_ITG7M

27 tutorials, and hopefully a 28th eventually, they break down all of what I know into the simplest of terms.

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Colon Philosophy

#11 Post by shadeclan » 17 Oct 2011 13:25

nitt wrote:http://www.youtube.com/watch?v=mWeh3_ITG7M

27 tutorials, and hopefully a 28th eventually, they break down all of what I know into the simplest of terms.

Nice. Thanks - I'm saving the link. Assembly appears to be a dying skill but I have great respect for those who take the time to learn and use it.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Colon Philosophy

#12 Post by dbenham » 25 Oct 2011 16:03

(original post deleted) Never mind - I was not distinguishing between GOTO LABEL vs. CALL LABEL.
Since GOTO LABEL without the colon works, I just assumed CALL LABEL works as well. Well it doesn't.

Dave Benham

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Colon Philosophy

#13 Post by shadeclan » 26 Oct 2011 06:40

dbenham wrote:(original post deleted) Never mind - I was not distinguishing between GOTO LABEL vs. CALL LABEL.
Since GOTO LABEL without the colon works, I just assumed CALL LABEL works as well. Well it doesn't.

Dave Benham

Yup.

Just for giggles, I tried calling a label in a batch job from a different batch job, ie: [call otherbatch.bat:otherlbl] - fortunately, it didn't work. I've learned a lot about batch scripting since I started frequenting this site - I've built beautiful libraries of routines I use every day. I would have been a little upset if I'd built the interface to those libraries for nothing or if there was a way to circumvent the interface - I do some error checking there.

shadeclan
Posts: 61
Joined: 02 Jun 2011 11:29
Location: USA - Somewhere between Albany NY and Bennington VT

Re: Colon Philosophy

#14 Post by shadeclan » 28 Oct 2011 12:11

Here's a thought. Since in the command "goto:eof" the colon is required, wouldn't it be a good idea to always use the colon for other labels so that, if you are a sieve-mind like myself, you don't forget it when coding "goto:eof"?

Image :lol:

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Colon Philosophy

#15 Post by aGerman » 28 Oct 2011 13:46

:eof is no label. It's only a virtual expression which tells the parser to jump to the end of file. For that reason only for goto :eof needs the colon.

Regards
aGerman

Post Reply