Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown operator [colon] used with while loop java

Tags:

java

operators

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.


1 Answers

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.

like image 185
Jesper Avatar answered Jun 26 '26 14:06

Jesper