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++.

3
  • possible duplicate of Examples of good gotos in C or C++ Commented May 12, 2015 at 17:11
  • No not a duplicate @KuramaYoko Commented May 12, 2015 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? Commented May 12, 2015 at 18:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

18 Comments

I really wish it were possible to remove the word Arguably from that first sentence. :-)
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
@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)
@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.
@JustSid: The linux kernel isn't written in C++.
|
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++.

1 Comment

Yes we did 4 loops : do-while, while, for, and if goto. I think i should drop the last one :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.