goto for an integer

Jun 26, 2017 at 12:43pm
closed account (1vf9z8AR)
How to do i use goto for an integer?

when i use goto label; label: it works but when i use goto 0; 0: it doesnt work.

I tried everything.:(
Jun 26, 2017 at 12:56pm
How to do i use goto for an integer?

What makes you think you can do that?

A goto label must be a valid C/C++ identifier and follow the rules for an identifier name. 0 is not a valid identifier.

BTW, gotos are bad practice and should be avoided.
Last edited on Jun 26, 2017 at 12:56pm
Jun 26, 2017 at 1:01pm
Hello suyashsing234,

First you should avoid using "goto" as it is bad programming practice.

When you use "goto 0;" it is looking for a label "0:" which I would guess you have not defined.

Post you code an I can give you a better idea of what to do. For now it is all a guess. Most times a "while" or "do/while" loop will eliminate a use of "goto".

Hope that helps,

Andy
Jun 26, 2017 at 1:50pm
The four jump statements in C++ are break, continue, return and goto. They are similar in the sense that all these statements unconditionally transfer control to some other part of the code. Of the lot, except for the return statement, the rest are best avoided, unless it is clear that there are no better alternatives available.

The jump statements break and continue do not accept labels; for these where to jump to is implicitly determined. For the jump statement goto, we have to specify a label to to jump to. This label is an identifier; the syntactic rules for all identifiers also apply to these labels (for instance, that they can't start with a digit).

The other situation where labels are used are in switch statements: however, the case label and the default label are not identifiers; the case label is the keyword case followed by an integral constant expression; and default is a keyword. The transfer of control to these labels is not unconditional (as in jump statements); the switch statement could be conceptually thought of as a conditional jump statement.
ie. switch(v) { ... case 5: ... } can be thought of as if( v == 5 ) goto case_label_5: ;. This is perhaps what triggered the question "How to do i use goto for an integer?"
Jun 26, 2017 at 2:26pm
closed account (1vf9z8AR)
so what should i use instead of goto which is also as simple as goto?

Btw i did my program without goto as Handy Andy suggested.
Last edited on Jun 26, 2017 at 2:28pm
Jun 26, 2017 at 3:30pm
> so what should i use instead of goto which is also as simple as goto?

There are situations where goto is the simplest and also the most elegant construct. However, these are quite rare.

It is difficult to answer the question in the absence of any context.
If you can provide a specific example where using goto appears to be most appropriate, we could discuss it.
Topic archived. No new replies allowed.