Using goto is OK?

Pages: 12
Previously:
Strange list - how to start?
http://www.cplusplus.com/forum/beginner/102439/



I tried to solve this question again using only simple statements.
The goto statement proved here very useful.

Is it good or bad to use goto statement?


My Code rewritten:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

int main ()
{
    int n;
    cout<<"n is an integer and \nn should be greater than 1\n";
    cout<<"n: ";
    cin>>n; // n should be greater than 1
    cout<<'\n';
    if (n==1) cout<<"1";
    else {
        int i;

        int* a = new int [n+1]; // a1=1, a2=2, ... ,an=n
        for(int j=1; j<=n; j++)
            a[j]=1;

        int* maxa = new int [n];
        for(int j=1; j<=n; j++) // MAXa1=1, MAXa2=2, ... ,MAXan=n
            maxa[j]=j;

        move_down:
            i=n;
            if (a[n]<=maxa[n]) {
                for(int j=1; j<=n; j++)
                    cout<<a[j]<<'\t'; //list
                cout<<'\n';
                ++a[n];
                goto move_down;
            }

        cout<<'\n';
        move_left:
            --i; if(i==1) goto endofprg;
            ++a[i];
            if(a[i]<=maxa[i]) {
                for(int k=i+1;k<=n;k++)
                    a[k] = a[i];
                goto move_down;
            }
            else goto move_left;

    }
    endofprg:
        cout<<"\n\nProgram reached its end.\n";

    return 0;
}
n is an integer and
n should be greater than 1
n: 5

1       1       1       1       1
1       1       1       1       2
1       1       1       1       3
1       1       1       1       4
1       1       1       1       5

1       1       1       2       2
1       1       1       2       3
1       1       1       2       4
1       1       1       2       5

1       1       1       3       3
1       1       1       3       4
1       1       1       3       5

1       1       1       4       4
1       1       1       4       5

1       1       2       2       2
1       1       2       2       3
1       1       2       2       4
1       1       2       2       5

1       1       2       3       3
1       1       2       3       4
1       1       2       3       5

1       1       2       4       4
1       1       2       4       5

1       1       3       3       3
1       1       3       3       4
1       1       3       3       5

1       1       3       4       4
1       1       3       4       5

1       2       2       2       2
1       2       2       2       3
1       2       2       2       4
1       2       2       2       5

1       2       2       3       3
1       2       2       3       4
1       2       2       3       5

1       2       2       4       4
1       2       2       4       5

1       2       3       3       3
1       2       3       3       4
1       2       3       3       5

1       2       3       4       4
1       2       3       4       5



Program reached its end.

Process returned 0 (0x0)   execution time : 3.234 s
Press any key to continue.


And please check if my code works well or there are mistakes?

goto statement does not allow to see the structure of the program. You should forget that there is such statement in C++ and never use it.
By the way your code is simply very bad. it is difficult to read and understand it.
The only reason 'goto' exists in C++ is for compatibility with libraries that are written in C. Everything that it does can be better accomplished with function calls and\or try, catch exceptions.
Is it good or bad to eat with your hands? Most people would say it's bad. Same with goto. Many people dislikes goto.

It is possible to eat a whole dinner with your bare hands and it is possible to write every loop in a big program using goto but it maybe it's not such a good idea.
@Peter87

Is it good or bad to eat with your hands?


Chicken are eaten with hands.:)
Last edited on
goto is not bad it is just misused. Learn what it does and how to use it. There is often a better way than using goto but sometimes you will find that it is a simple way to achieve something.

PS: It is not worth asking for an example of a good time to use goto as such an example would be non-trivial and any trivial example would be met with a counter "do it without goto like this"
@Grey Wolf

.... but sometimes you will find that it is a simple way to achieve something.


These "sometimes" occur only when the programmer has no brain that is when he has a very low qualification. The less qualification the more often such a programmer will find "a simple way". And the original post shows such a simple way.:)
@Vlad
What about this type of use?

1
2
3
4
5
for (int i = 0; i < 10; ++i) 
    for (int j = 0; j < 10; ++j) 
        if ( (x = T[i][j].g()) < e ) 
            goto end_for_for;
end_for_for:


It's not real world actual code, but imagine you're in a real world situation where you need to break out of a nested loop.
Last edited on
Well vlad, my advice:
Don't use goto until you have learnt enough to know why using goto is 'bad', then you will have learnt enough to us goto properly.
@htirwin

@Vlad
What about this type of use?

for (int i = 0; i < 10; ++i)
for (int j = 0; j < 10; ++j)
if ( (x = T[i][j].g()) < e )
goto end_for_for;
end_for_for:


As I said this demonstrates only that the programmist has a low qualification and does not know at least standard algorithms.
@Vlad.
So you're saying that a qualified programmer will never be in a situation where they need to, or where it would be best to, break out of a nested loop?
Last edited on
At least you could write

1
2
3
4
5
6
7
8
const int N = 10;

for (int i = 0; i < N; ++i)
{
   int j = 0; 
   while ( ( j < N ) && !( ( x = T[i][j].g() ) < e ) ) ++j;
   if ( j != N ) break;
}


And the internal loop could be for example substituted for an algorithm,
@ htirwin: I'm going to side with vlad here. Program flow control isn't something that should be ignored just because you have an instruction that can jump out of a loop.

This is your sample:
1
2
3
4
5
for (int i = 0; i < 10; ++i) 
    for (int j = 0; j < 10; ++j) 
        if ( (x = T[i][j].g()) < e ) 
            goto end_for_for;
end_for_for:


This is my version of a solution that isn't context specific:
1
2
3
4
5
6
7
8
9
void* ArbitraryObject::MyNestedForLoops(int i, int j, ...)
{
for (i = 0; i < 10; ++i) 
    for (j = 0; j < 10; ++j) 
        if ( (x = T[i][j].g()) < e )
        {
            return T[i][j].g();
        }
}


This is what Grey Wolf meant when he said "It is not worth asking for an example of a good time to use goto as such an example would be non-trivial and any trivial example would be met with a counter "do it without goto like this" ".

EDIT: I cleared up my redeclarations.
Last edited on
@htirwin

@Vlad.
So you're saying that a qualified programmer will never be in a situation where they need to, or where it would be best to, break out of a nested loop?


You are mistaken. I said that 1) a qualified programmer never use the goto statement.. And 2) the less qualification has a programmer the more often he will find a "simple way" with using the goto statement.

You are not the first and are not the last who are trying to reanimate the goto statement due to your own low qualification.
I advice you to reread the original post. There is written

I tried to solve this question again using only simple statements.
The goto statement proved here very useful.



What is the qualification of the author of the topic? He is a beginner. And you are not very far from him.

He "proved" here that his goto is very useful As for me I consider his code as a very and very bad code.
Last edited on
I still think goto is favorable for that purpose compared to vlads workaround. It's much less obfuscated.

I never said the OP's use is good. The reason it is bad is because it is obfuscated. That is the same reason your work around is bad.
Last edited on
@computergeek01

That may be the way to go many times, but in the one single use I have found for goto, in my 3 thousand line program, I opted not to make a separate function to avoid use of goto.

The reason being that the method where it is used, is small and the nested loop is the main part, and there are a few lines of code after the nested loop that need to get executed.

Breaking it up into multiple functions would only add a new method that by itself is meaningless, and that would not see reuse in any other context.
Last edited on
I'm not saying that you're wrong or that you're a bad programmer. I was only trying to illustrate Grey Wolfs point, and in fact I may even expand on it. Would you like to see make an argument that says a real programmer should never have to use if else statements? Because I'm fairly confident I can do it.

I dislike goto because it makes code hard to read, it's use actually involves more work then most of the alternative methods that accomplish the same thing and new users get complacent with using it.
Here's my view:

1. Beginners should not use goto - there is almost always a better way. Learn to program properly.
2. Experts can use goto in situations that warrant it. Being an expert allows them (not me) to recognise when it is feasible or appropriate to use a goto.

JLBorges nailed here, I think:

http://www.cplusplus.com/forum/general/93381/#msg502210
I'd put it this way:

The more dangerous, and damaging to maintainability, a particular practice is, then the more experienced a developer has to be before they should consider using it.

"goto" is such a dangerous construct, and can have such a negative impact on maintainability, that almost nobody who attempts to use it is experienced enough that they should be using it.

Pages: 12