DEV Community

Discussion on: Did you know that JavaScript has had labels since ES3?!?

Collapse
 
aptituz profile image
Patrick Schönfeld

Uh. That's even worse. Now instead of a descriptive label, one has to count the nested loops to find out what the heck this statement affects.

Thread Thread
 
aidantwoods profile image
Aidan Woods

I don't prefer it, just stating it's there ;-)

My favourite is Swift that has continue label or break label to refer to what to continue or break, but no accompanying goto which I think is an important safety decision.

developer.apple.com/library/conten...

gameLoop: while square != finalSquare {
    diceRoll += 1
    if diceRoll == 7 { diceRoll = 1 }
    switch square + diceRoll {
    case finalSquare:
        // diceRoll will move us to the final square, so the game is over
        break gameLoop
    case let newSquare where newSquare > finalSquare:
        // diceRoll will move us beyond the final square, so roll again
        continue gameLoop
    default:
        // this is a valid move, so find out its effect
        square += diceRoll
        square += board[square]
    }
}
print("Game over!")