0

I wanted to ask if this is correct goto loop in C++ :

#include <iostream>

int main() {
    int i=0, a=0;
    this:std::cout << i << " is less than 10\n";
    i++;
    if( i<10) goto this;
    return 0;
}

I have this in very old c++ book, and don't know if it is correct in present day C++.

Note: It compiles successfully on Linux mint using g++.

  • possible duplicate of [Examples of good gotos in C or C++](http://stackoverflow.com/questions/245742/examples-of-good-gotos-in-c-or-c) – gibertoni May 12 '15 at 17:11
  • No not a duplicate @KuramaYoko – Doomsday 666 May 12 '15 at 17:15
  • Since `this` is a reserved word in C++, I wouldn't expect that sucker to compile. Actually, I just tried. I can't compile it. I can with `that` in place of `this`. What does the command line look like? – user4581301 May 12 '15 at 18:38

2 Answers2

7

Arguably, there's no proper way to use goto. Use a structured loop instead:

for (int i = 0; i < 10; ++i) {    
    std::cout << i << " is less than 10\n";
}

If you insist on using goto, then you'll have to change the name of the label. this is a keyword in C++, and can't be used as an identifier.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    I really wish it were possible to remove the word *Arguably* from that first sentence. :-) – Ken White May 12 '15 at 17:09
  • But sir we are taught from old book and our instructor says that if goto loops are important. Are you saying that they are non standard? – Doomsday 666 May 12 '15 at 17:09
  • 3
    I would argue for `goto` having a proper usage. Linus makes a compelling case for the usage in the Linux kernel. But yeah, this right here is not one of those good use cases for goto – JustSid May 12 '15 at 17:09
  • 3
    @Doomsday666 How old is your professor? And no they are not standard, in that 99% of the time, people use for/while/do-while loops. (See JustSid's comment for an example of the 1% of cases) – Borgleader May 12 '15 at 17:10
  • 1
    @Doomsday666: `goto` is standard, but there's never a good reason to use it in C++ except for obfuscation. I'd drop your course; it will be a complete waste of time. If you want to learn C++, get a [good modern book](http://stackoverflow.com/questions/388242). – Mike Seymour May 12 '15 at 17:11
  • 5
    @JustSid: The linux kernel isn't written in C++. – Mike Seymour May 12 '15 at 17:12
  • @MikeSeymour I never claimed it was. But the basic case Linus makes also applies to a kernel written in C++, since usually you'll end up disabling exceptions and other constructs that don't really work well in a kernel. – JustSid May 12 '15 at 17:14
  • @Doomsday666: StackOverflow is a good resource. So is http://en.cppreference.com/. But this isn't the place for recommendations. – Mike Seymour May 12 '15 at 17:19
  • Another possible use case for goto statement is, when there are multiple exit points in the program and you want to exit only from a single point in the code. I agree example shown here is not a good use case though – irsis May 12 '15 at 17:19
  • @Rahul You have described a bad approach of using goto that actually demonstrates one more that goto statements shall not be used. The problem with your use case is that different places in the program require different clean up operations. So your "one single point of exit" in realty only produces multiple goto statements and multiple labels before this "single exit point".:) – Vlad from Moscow May 12 '15 at 17:40
  • @VladfromMoscow - common exit points, mostly used in C (versus C++), typically have a series of statements equivalent to if(handle != invalid)close (handle); or if(pointer != NULL)free(pointer);, so the only goto's are the ones that branch to the common exit point. – rcgldr May 12 '15 at 20:21
  • @rcgldr Such bad code is written by beginners who is unable to write code. – Vlad from Moscow May 12 '15 at 20:23
  • @VladfromMoscow - That's a matter of opinion. I've seen examples of common exit points used by expert programmers for custom operating systems, and in embedded applications. This is a bit off topic, there are other threads and/or web sites to argue the pros and cons of using goto's. The question here is apparently an old and not a good example. – rcgldr May 12 '15 at 20:31
  • @rcgldr Many so-called "expert programmers" write code on the level of beginners. – Vlad from Moscow May 12 '15 at 20:35
  • @VladfromMoscow - Donald Knuth .... disagreed with abolishing the GOTO statement. [wiki structured programming](http://en.wikipedia.org/wiki/Structured_programming) . I don't think he's considered a beginner. – rcgldr May 13 '15 at 02:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77670/discussion-between-rcgldr-and-vlad-from-moscow). – rcgldr May 13 '15 at 02:45
  • @rcgldr: Knuth wasn't talking about C++. – Mike Seymour May 13 '15 at 11:39
  • @KenWhite: So do I. But, as these comments prove, there are still those who argue for a spaghetti structure, after decades spent developing better options. So arguable it is, and arguable it's likely to remain. – Mike Seymour May 13 '15 at 14:04
1

My advice is forget that C++ has the goto statement and never use it. :) When the goto statement is used the program loses its structure and as result such programs are difficult to read. Also one goto statement usually produces another goto statements in the same program because the discipline of writing the structured code is broken.:) And usually it is dificult to modify such programs.

The program that you showed can be rewritten the following way

#include <iostream>

int main() 
{
    const int N = 10;
    int i = 0;

    do
    { 
       std::cout << i << " is less than " << N << "\n";
    } while ( ++i < N );

    return 0;
}

Or the following way

#include <iostream>

int main() 
{
    const int N = 10;

    for ( int i = 0; i < N; i++ )
    { 
       std::cout << i << " is less than " << N << "\n";
    }

    return 0;
}

Though in general case when N can be initially set to 0 the two programs are not equivalent.

Take into account that variable a is not used in your porgram and its declaration should be removed.

Also it is a bad idea to use keywords as identifiers. this is a reserved keyword in C++.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335