user3844143
user3844143

Reputation:

Unknown operator [colon] used with while loop java

while browsing some source code i noticed a weird operator being used on a while loop

topLoop: while(dist < 0){ random code... }

what does the 'topLoop:' mean in this situation? and what does it do to the while exactly? p.s. topLoop is just a int defined earlier on in the code.

Upvotes: 3

Views: 345

Answers (1)

Jesper
Jesper

Reputation: 206916

The colon is not an operator. The topLoop: is called a label. You can use it, for example, to directly jump from a nested loop to outside the outer loop.

There's probably a break topLoop; statement somewhere inside the loop that you didn't show. This will make execution jump to the topLoop label.

Labels are used only seldomly in practice, and in my opinion using labels is bad practice - they are a kind of goto statement, and using them can quickly make your code a hard to understand, tangled mess.

p.s. topLoop is just a int defined earlier on in the code.

Maybe the code has a variable named topLoop, but this has nothing to do with the label that happens to have the same name.

Upvotes: 4

Related Questions