tags:

views:

123

answers:

4

I was reading some c++ code, and i saw something interesting.

The code was something like this:

repeat:
    ...code here....
fallback:
    ...code here....
start:
        ....another code....

This is the first time i am seeing this kind of "labels" in c++ code, i called the labels cos i have seen something similar in assembly code where the code is divided into sections with different titles which end with colon.

I am asking you what does that mean, and of what use it can be ?

+6  A: 

It is a label, to which you can jump using a goto.

Whether one should use gotos in a program is another matter entirely.

James McNellis
Hmm..how come i haven't seen goto in any c++ code i've seen ?
dada
@dada did you see any `switch`? :-)
Pavel Shved
'cause it's baaaaaaaad
Harald Scheirich
Because `goto`s tend to be discouraged in C and C++.
Chinmay Kanchi
@dada: You can find the answer to that by searching for "goto" and "harmful" together on either StackOverflow or Google. I guarantee there will be many pages debating the utility of `goto`.
James McNellis
@Pavel: No, no switch anywhere.
dada
@dada - because the number of times that a `goto` is the right flow control construct is very, very small. It's almost always the sign of some serious spaghetti code.
tvanfosson
Because many people frown on them; they can produce **very** obscure code. (OTOH, not all the things that people propose to replace the use of `goto` are necessarily clearer either. Some code is just nasty.)
Donal Fellows
@dada : Rather more important is whether or not you see goto in your code. If you see, be happy for all the reasons quoted by others
hype
+1  A: 

A label is generally the target of a goto in C++.

bobbymcr
A: 

Labels are used as targets for goto, but, if you put a label, you are not forced to use goto, if you do not see any goto in the code you are reading, the people/guy who wrote that code probably used them for actually labeling purposes (duh!).

Francisco Soto
Why not use a comment?
the_drow
I have no idea, is the only thing I can imagine if they are using labels but not using goto. Maybe their editor lets them fold the code at labels, who knows.
Francisco Soto
A: 

Labels are used with goto and switch/case statements, when they are used to direct the flow of control. However, labels may also be used absent any goto statements (case labels must only appear in a switch statement) as means of identifying particular code segments -- i.e., somewhat like a comment, though in reality more like a title. If you're not seeing any switch or goto statements, I suspect the code author is simply using them to organize his code.

tvanfosson