Last active
June 16, 2024 15:11
-
-
Save shibukawa/315765020c34f4543665 to your computer and use it in GitHub Desktop.
goto for JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
first: | |
printf("first\n") | |
goto third; | |
second: | |
printf("second\n"); | |
thrid: | |
printf("third\n"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var destination = "first" | |
topLabel: while (true) { | |
var jump = false; | |
switch (destination) { | |
case "first": // caseがラベル相当 | |
console.log("first"); | |
//ここから | |
destination = "third"; | |
jump = true; | |
break; | |
//ここまでが goto third; | |
case "second": | |
console.log("second"); | |
case "third": | |
console.log("third"); | |
} | |
if (jump) { | |
continue topLabel; | |
} | |
break; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OutJump(Exception): | |
pass | |
outjump = OutJump() | |
destination = "first" | |
while True: | |
try: | |
if destination == "first": | |
print("first") | |
destination = "third" | |
raise outjump | |
elif destination == "second": | |
print("second") | |
elif destination == "third": | |
print("third") | |
except OutJump as e: | |
pass | |
else: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment