0
How can i use goto in this programming languages ?
Skipping from one part of the program to another part!!!
12 Antworten
+ 2
Python:
Throw an exception then catch it where you want to jump. It's Pythony.
c++:
break/continue function like this (also, see c#). Or use labels (nearby!)...and see comments on this "never/ever goto" response:
http://stackoverflow.com/a/379259/3981745
('Nothing better than goto for nested loops' and all the upvotes)
Also refactoring and gotcha's (like cleaning up multiple exits):
http://stackoverflow.com/a/379189/3981745
Otherwise I suppose you'd have to update the instruction or return pointer. Normally only malware does this.
java:
Java's all finicky about security; goto's defined but unimplemented. There's an Unsafe class but I can't say that's the ticket for sure, mostly because I was in Android's Java, not Sun's...but I found this and it looks clever:
http://stackoverflow.com/a/26431031/3981745
Summary: Identifiers followed by colons and break/continue to them like labels.
c#:
https://msdn.microsoft.com/en-us/library/13940fs2.aspx
"A common use of goto is to transfer control to a specific switch-case label or the default label in a switch statement.
The goto statement is also useful to get out of deeply nested loops."
Note, this major vulnerability arose while using goto:
https://www.theguardian.com/technology/2014/feb/25/apples-ssl-iphone-vulnerability-how-did-it-happen-and-what-next
And remember, in machine language even your 'if' statements are jumps (gotos). You use them all the time; you just let something better at it than you handle them.
+ 2
updated answer to include c++ links, labels, comments.
+ 2
@Awodire. I still find your description confusing, but I will attempt an answer to see if we understand one another.
You say you want certain code to execute in "one click". If I understand (please correct me if I am wrong) you want certain code to execute when the user clicks an object (a menu item, a button, an icon …).
Generally, we create a user interface from objects in a framework or library. When interactive objects are created there is a way to specify an event handler for the object.You create a function to contain the desired code and include the name of the function as a parameter to the class constructor when creating the object. Then the GUI will call the function when the object is clicked (double clicked, right clicked, dragged, dropped or some other event occurs). Depending on the framework, the function will need specific parameters in a defined order so the function can figure out which object was activated and what type of event occurred. By testing these parameters with 'if' statements the function can select the code to execute.
In some cases one function can be designed to handle multiple events from multiple objects, if they have enough in common that it makes sense to do so. In other cases, it may be better to write a single function for each object/event. Most likely, you will have a set of functions that work together to get the job done efficiently while reducing the total amount of coding you need to do.
For example, in HTML you could have the following:
<img alt="…" src="…" id="imageId" onclick='func("imageId")' />
The function would be defined in a JavaScript and you code it to do whatever needs to happen when the image is clicked. Passing the 'imageId' as a parameter to 'func' tells 'func' which image was clicked and 'func' can manipulate the image via the DOM.
Did I understand your question properly? Does my answer make sense to you?
+ 1
Avoid GOTO, like totally.
+ 1
There is no GOTO in those languages. GOTO is unnecessary. GOTO compromises the maintainability and quality of code (programs). You should never use GOTO if you hope to code professionally.
+ 1
Also your code will be sneered upon
+ 1
@Awodire I don't know what you mean by "jump from one top and flip back down". Would you give an example?
0
@gordon okay ....you want every line of statement in your code to be short and precise and flexible to get back to in one click without a Goto as you said earlier.."To be professional"
- 1
@gordon how do you jump from one top and flip back down with just a simple "code" ??
- 1
@kirk be more explicit about c++ .....how can go to be done there or it can't be applied???
- 1
@gordon I was a bit ignorant about programming then ...i can feel the gravity of your explanation......... thanks a lot bro!!!!
- 1
@kirk thanks I also find your answer useful enough!!!!!