How do you get back to a specific line in Java?

I am trying to make a Math Calculator application. However, I am wondering if there is an instruction to return to a specific line?

Let me clarify:

I do not mean a cycle. Scenario is possible here: let's say that the user launched the program and reached line 54. If I have an if-else statement, if there is a way I could make it so that "if (input = 0) {go back to line 23}

Is there anything that can allow me to do this, not including the loop?

+5
source share
3 answers

Java does not have goto (goto is a reserved word, but not used). Think about how your approach and language choices fit together. Probably the best way to do this. Consider extracting a method or using a flag inside a loop. Without further information, guidance will be limited.

+4
source

Why can't you use a loop?

Put your code in the function, and then enter the loop that executes during input = 0.

+1
source

Nope.

The goto operator exists in C and C ++, which Java is vaguely similar to, but is usually considered very bad practice. This makes reading and debugging code difficult. Here are some good ways to solve this problem with more structured programming:

 do { ... } while (input == 0); 
 private void doTheThing() { // please use a better name! ... if (input == 0) doTheThing(); // recursion not recommended; see alternate // method below } // alternate method: do { doTheThing(); } while (input == 0); 
+1
source

Source: https://habr.com/ru/post/1206767/


All Articles