Question:
Does the programming language have a jump command?
James Geddes
2007-12-02 10:35:24 UTC
In assembly, you can tell the program to jump back to a particular point in the program, can you do that in C?
Seven answers:
mousymite
2007-12-02 10:47:29 UTC
C supports a goto statement and labels to allow jumps. Remember, however, that they should only be used for a few particular kinds of problems (see the Knuth reference in the Wikipedia article cited). Using it whenever it seems convenient can lead to some truly awful programming. You can almost always use other, more structured, more easily debuggable and maintained constructs to accomplish the same thing.
2007-12-02 22:10:51 UTC
I'm not much of a C programmer, but I know how to do it in BASIC. You label a part of the program with anything you want and follow the label with a ":"

Example:



first: 'this is the label

CLS

PRINT "Choose as best answer"

IF answer=best THEN

LOCATE 12,40

PRINT "WOOOOOOOO"

ELSE GOTO first

END IF



This probably doesn't help you much, but......
Balk
2007-12-02 18:53:20 UTC
Yes. You use the 'goto' instruction.

Example:



if (some_label== 1)

goto foo;

(more C code)



foo:

(C code)



One thing to keep in mind is that you cannot access the memory address of C code labels.

For example, in Assembly, I can get the address of any label in my code, like this:



label1:

(some ASM code)

mov edx,offset label1 ;EDX holds the code address of label 1



In C with built-in Assembly language, this would not work:



goto mylabel;



mylabel:

(some C code)

_asm {

mov edx,offset mylabel /* error! 'mylabel' not found */

}

/* You can't get the address of mylabel. You can only access C data labels. You CAN get the address to a C procedure though. */



P.S.

Since you know Assembly language, you know that using GOTO in C/C++ can be more efficient than trying to avoid using it. Those who don't know this should learn assembly language so they can stop writing bloated, slow code.
Runa
2007-12-02 18:49:07 UTC
To do this in C, you use function calls. *Never* use goto, that will make a mess of your programs. just call the function and control will be transferred to that function and returned to the point of call after execution is complete.
JOHN R
2007-12-02 18:51:04 UTC
Yes you can. There are a number of ways of doing it. It depends what you are trying to achieve.
2007-12-02 19:30:22 UTC
Depends what IDE you are using.

I suppose breakpoints work.
Brian C
2007-12-02 18:40:31 UTC
yeahhhh you can,



uh i think its goto or something


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...