goto

How to avoid use of goto and break nested loops efficiently [closed]

﹥>﹥吖頭↗ 提交于 2019-11-29 10:44:24
问题 I'd say that it's a fact that using goto is considered a bad practice when it comes to programming in C/C++. However, given the following code for (i = 0; i < N; ++i) { for (j = 0; j < N; j++) { for (k = 0; k < N; ++k) { ... if (condition) goto out; ... } } } out: ... I wonder how to achieve the same behavior efficiently not using goto . What i mean is that we could do something like checking condition at the end of every loop, for example, but AFAIK goto will generate just one assembly

C# Puzzle : Reachable goto pointing to an unreachable label

半腔热情 提交于 2019-11-29 07:58:10
Here's Eric Lippert's comment from this post : Now that you know the answer, you can solve this puzzle: write me a program in which there is a reachable goto which goes to an unreachable label. – Eric Lippert Jul 17 at 7:17 I am not able to create a code which will have reachable goto pointing to an unreachable label. Is that even possible? If yes, what would the C# code look like? Note: Let's not get into discussion about how 'goto' is bad etc. This is a theoretical exercise. My original answer: try { goto ILikeCheese; } finally { throw new InvalidOperationException("You only have cottage

goto in Java bytecode

橙三吉。 提交于 2019-11-29 03:06:58
So the other day when I was looking at the wikipedia page for Java bytecode I came across this example: Consider the following Java code: outer: for (int i = 2; i < 1000; i++) { for (int j = 2; j < i; j++) { if (i % j == 0) continue outer; } System.out.println (i); } A Java compiler might translate the Java code above into byte code as follows, assuming the above was put in a method: 0: iconst_2 1: istore_1 2: iload_1 3: sipush 1000 6: if_icmpge 44 9: iconst_2 10: istore_2 11: iload_2 12: iload_1 13: if_icmpge 31 16: iload_1 17: iload_2 18: irem 19: ifne 25 22: goto 38 25: iinc 2, 1 28: goto

How to store goto labels in an array and then jump to them?

坚强是说给别人听的谎言 提交于 2019-11-29 02:18:53
问题 I want to declare an array of "jumplabels". Then I want to jump to a "jumplabel" in this array. But I have not any idea how to do this. It should look like the following code: function() { "gotolabel" s[3]; s[0] = s0; s[1] = s1; s[2] = s2; s0: .... goto s[v]; s1: .... goto s[v]; s2: .... goto s[v]; } Does anyone have a idea how to perform this? 回答1: It is possible with GCC feature known as "labels as values". void *s[3] = {&&s0, &&s1, &&s2}; if (n >= 0 && n <=2) goto *s[n]; s0: ... s1: ... s2

To use goto or not?

大城市里の小女人 提交于 2019-11-28 21:17:55
This question may sound cliched, but I am in a situation here. I am trying to implement a finite state automaton to parse a certain string in C. As I started writing the code, I realised the code may be more readable if I used labels to mark the different states and use goto to jump from one state to another as the case comes. Using the standard breaks and flag variables is quite cumbersome in this case and hard to keep track of the state. What approach is better? More than anything else I am worried it may leave a bad impression on my boss, as I am on an internship. Using a goto for

Other ways to deal with “loop initialization” in C#

丶灬走出姿态 提交于 2019-11-28 20:13:24
问题 To start with I'll say that I agree that goto statements are largely made irrelevant by higher level constructs in modern programming languages and shouldn't be used when a suitable substitute is available. I was re-reading an original edition of Steve McConnell's Code Complete recently and had forgotten about his suggestion for a common coding problem. I had read it years ago when I was first getting started and don't think I realized how useful the recipe would be. The coding problem is the

Statement goto can not cross variable definition?

非 Y 不嫁゛ 提交于 2019-11-28 19:54:37
问题 Suppose these code compiled in g++ : #include <stdlib.h> int main() { int a =0; goto exit; int *b = NULL; exit: return 0; } g++ will throw errors: goto_test.c:10:1: error: jump to label ‘exit’ [-fpermissive] goto_test.c:6:10: error: from here [-fpermissive] goto_test.c:8:10: error: crosses initialization of ‘int* b’ It seems like that the goto can not cross pointer definition, but gcc compiles them ok, nothing complained. After fixed the error, we must declare all the pointers before any of

`goto` in Python

元气小坏坏 提交于 2019-11-28 17:09:11
问题 I must use goto in Python. I found entrians goto but my Python implementation (CPython 2.7.1 on Mac) does not have this module, so it doesn't seem to be portable. It should at least work in all Python implementations which support CPython bytecode (esp. I care about CPython and PyPy). Then there is this related question, and cdjc's goto. And the ones given by answers below. I could go and build up the bytecode manually (i.e. write my own Python compiler) because there is such an instruction (

Being pressured to GOTO the dark-side

筅森魡賤 提交于 2019-11-28 16:56:42
问题 We have a situation at work where developers working on a legacy (core) system are being pressured into using GOTO statements when adding new features into existing code that is already infected with spaghetti code. Now, I understand there may be arguments for using 'just one little GOTO' instead of spending the time on refactoring to a more maintainable solution. The issue is, this isolated 'just one little GOTO' isn't so isolated. At least once every week or so there is a new 'one little

goto label in the same loop in Bison

隐身守侯 提交于 2019-11-28 14:22:36
I am making a parser with Bison and Flex and I want to create a "goto label" statement, but I want to check if the label exists in the same block of code (between brackets { }, loop, etc). Is there a function that checks such things? Your question implies that you are missing some background context in building language translators/compilers, so perhaps a small tutorial will assist you in solving your problem. I hope you don't mind. The processing of computer languages is conventionally divided up into a sequence of steps (sometimes called phases or passes). Each step handles a component of