goto

C# Puzzle : Reachable goto pointing to an unreachable label

雨燕双飞 提交于 2019-11-28 01:20:08
问题 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. 回答1: My original

PHP and the goto statement to be added in PHP 5.3

人走茶凉 提交于 2019-11-27 20:10:22
The "goto" statement comes straight out of ASM or any other assembler language. Here's a link: http://be2.php.net/manual/en/control-structures.goto.php I'm wondering: what can this do to make my code more well-organized? How can I implement this in larger projects, without screwing it up. Since the goto will allow you to jump back and forth, accidental assignments and infinite loops are waiting to happen if you use this the wrong way. Can someone give me an example of a good use of this? EDIT: allright, I've seen some of the replies and apparently a wide consensus exists about the use of the

Variable declaration after goto Label

拟墨画扇 提交于 2019-11-27 17:56:27
Today I found one interesting thing. I didn't know that one can't declare a variable after a goto label. Compiling the following code #include <stdio.h> int main() { int x = 5; goto JUMP; printf("x is : %d\n",x); JUMP: int a = 0; <=== giving me all sorts of error.. printf("%d",a); } gives errors like temp.c: In function ‘main’: temp.c:7: error: expected expression before ‘int’ temp.c:8: error: ‘a’ undeclared (first use in this function) temp.c:8: error: (Each undeclared identifier is reported only once temp.c:8: error: for each function it appears in.) Now what is the logic behind that? I

Is it possible to store the address of a label in a variable and use goto to jump to it?

狂风中的少年 提交于 2019-11-27 17:29:58
I know everyone hates gotos. In my code, for reasons I have considered and am comfortable with, they provide an effective solution (ie I'm not looking for "don't do that" as an answer, I understand your reservations, and understand why I am using them anyway). So far they have been fantastic, but I want to expand the functionality in such a way that requires me to essentially be able to store pointers to the labels, then go to them later. If this code worked, it would represent the type of functionality that I need. But it doesn't work, and 30 min of googling hasn't revealed anything. Does

Is there a goto statement in Java?

不问归期 提交于 2019-11-27 16:04:02
I'm confused about this. Most of us have been told that there isn't any goto statement in Java. But I found that it is one of the keywords in Java. Where can it be used? If it can not be used, then why was it included in Java as a keyword? The Java keyword list specifies the goto keyword, but it is marked as "not used". It was in the original JVM (see answer by @VitaliiFedorenko ), but then removed. It was probably kept as a reserved keyword in case it were to be added to a later version of Java. If goto was not on the list, and it gets added to the language later on, existing code that used

Jump command in MATLAB

瘦欲@ 提交于 2019-11-27 16:02:35
I'm working with the m-file editor of MATLAB and I need to jump from one line to another. If I need to jump from inside a For ... end , I can't use the usual "while" technique. Is there anyway to jump from a line to another, like goto in C? There is no goto statement in MATLAB, but there are a few other commands for use with loops that may help you: continue : This statement will skip the remaining commands in a for or while loop and move on to the next iteration. break : This statement will terminate execution of a for or while loop. There is no goto in matlab. However, this does not say you

Why does this “finally” execute?

你说的曾经没有我的故事 提交于 2019-11-27 15:52:56
问题 If you run the code below it actually executes the finally after every call to the goto: int i = 0; Found: i++; try { throw new Exception(); } catch (Exception) { goto Found; } finally { Console.Write("{0}\t", i); } Why? 回答1: Why do you expect it to not execute? If you have try/catch/finally or try/finally block, finally block executes no matter what code you may have in the try or catch block most of the time. Instead of goto, consider 'return'. //imagine this try/catch/finally block is

When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto (in C)?

Deadly 提交于 2019-11-27 13:30:24
When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto ? Thanks, Chenz They are equivalent, even if you turn the optimizer off. Example: #include <stdio.h> extern void f(void) { while(1) { putchar(' '); } } extern void g(void) { for(;;){ putchar(' '); } } extern void h(void) { z: putchar(' '); goto z; } Compile with gcc -O0 gives equivalent assembly for all 3 functions: f: ; [ EXTERNAL ] ; +00000 00000fb4 80402DE9 stmdb sp!,{r7,lr} +00004 00000fb8 00708DE2 add r7,sp,#0x0 +00008 00000fbc 2000A0E3 loc_000008: mov r0,#0x20 +0000c 00000fc0 0A0000EB bl

Use a 'goto' in a switch?

喜夏-厌秋 提交于 2019-11-27 13:01:27
I've seen a suggested coding standard that reads Never use goto unless in a switch statement fall-through . I don't follow. What exactly would this 'exception' case look like, that justifies a goto ? Mehrdad Afshari This construct is illegal in C#: switch (variable) { case 2: Console.WriteLine("variable is >= 2"); case 1: Console.WriteLine("variable is >= 1"); } In C++, it would run both lines if variable = 2 . It may be intentional but it's too easy to forget break; at the end of the first case label. For this reason, they have made it illegal in C#. To mimic the fall through behavior, you

windows batch file with goto command not working

ぐ巨炮叔叔 提交于 2019-11-27 08:59:26
I have a problem with GOTO command and affiliated labels. Facts: Given a bunch of files from a folder (they are log errors) I need to open them and check if they contain a specific string. If yes then eliminate some characters (all the chars after the last appearance of "_", including itself) from the file names and do other operations. For cutting off the chars I'm using GOTO command in a loop manner as I found it described here: http://www.robvanderwoude.com/battech_while_loops.php The script is: @echo off setlocal EnableDelayedExpansion cls for %%X in (D:\e-pub\outbox\logs\*.*) do ( for /F