3

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 ?

dada
  • 1,095
  • 4
  • 13
  • 14

4 Answers4

15

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
  • 348,265
  • 75
  • 913
  • 977
  • Hmm..how come i haven't seen goto in any c++ code i've seen ? – dada May 01 '10 at 17:59
  • @dada did you see any `switch`? :-) – P Shved May 01 '10 at 18:00
  • Because `goto`s tend to be discouraged in C and C++. – Chinmay Kanchi May 01 '10 at 18:00
  • @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 May 01 '10 at 18:00
  • @Pavel: No, no switch anywhere. – dada May 01 '10 at 18:01
  • @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 May 01 '10 at 18:01
  • 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 May 01 '10 at 18:01
  • @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 – mukeshkumar May 01 '10 at 18:16
  • Wow. I was watching [this video](https://www.youtube.com/watch?v=ZTqHjjm86Bw), then at [22:10](https://youtu.be/ZTqHjjm86Bw?t=1330) I asked myself _"what is `bla:` in C++?"_, searched on StackOverflow, and found this answer... And guess who's the author of the video? What a coincidence! – Enlico Jan 18 '22 at 20:00
2

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

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
0

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
  • 10,277
  • 2
  • 37
  • 46
  • 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 May 01 '10 at 18:39
0

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
  • 524,688
  • 99
  • 697
  • 795