Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:09:09+00:00 2026-05-31T12:09:09+00:00

The following code: #include <cstdlib> #include <iostream> using namespace std; int function(void) { static

  • 0

The following code:

#include <cstdlib>
#include <iostream>
using namespace std;
int function(void)
{
    static int i,state=0;
    switch(state)
    {
         case 0: goto labeL0;
         case 1 :goto labeL1;         

    }

    labeL0:
    for (i = 0; i < 10; i++)
    {
        state=1;
        return i;
        labeL1:;
    }

}
int main(int argc, char *argv[])
{
    cout << function() << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

fails. I mean it returns only 0 instead of 0,1,2,...

I wanted just use label and goto statements to implement such functions. It is for practice (let’s say homework), but I can’t get it to work. Is this even possible?

How can I use goto and label statements so that this function prints 0 1 2... so on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:09:10+00:00Added an answer on May 31, 2026 at 12:09 pm

    It’s not clear to me exactly what you’re trying to do. If your goal is
    jsut to use goto, the simplest solution is to implement the algorithm
    exactly as you’ld normally do, replacing looping constructs wit goto;
    i.e. instead of:

    for ( int i = 0; i < 10; ++ i ) {
        std::cout << i << std::endl
    }
    

    you could write:

        int i = 0;
        goto label1:
    label2:
            std::cout << i << std::endl;
            ++ i;
    label1:
            if ( i < 10 ) goto label2;
    

    Back in the old days, with Fortran IV, this is what we actually did.
    There’s absolutely no reason to do it today (except maybe obfuscation).

    I wonder, however, given the static variables, if you’re not trying to
    implement some sort of co-routine; that each time you call the function,
    you output one higher than the previous time. In this case, I’d
    recommend maintaining the state in a class, rather than using static
    variables. In addition the function will need some sort of return value
    so that the caller will know when it’s finished, and the caller will
    have to loop. Something like the following should do the trick:

    class CoRoutine
    {
        int i;
    public:
        CoRoutine() : i( 0 ) {}
        bool function()
        {
            if ( i < 10 ) {
                std::cout << i <<std::endl;
                ++ i;
            }
            return i < 10;
        }
    };
    
    int
    main()
    {
        CoRoutine c;
        while ( c.function() ) {
        }
        return 0;
    }
    

    (There’s still no need for goto, of course.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following code #include <iostream> #include <cstdlib> using namespace std; int main() {
The following code #include <iostream> using namespace std; int main() { const char* const
Consider the following code: #include <iostream> using namespace std; int main() { int x,
the following code #include <iostream> using namespace std; int main(){ char greeting[50] = goodmorning
If I write the following code: #include <iostream> using namespace std; int main() {
I’ve got the following code: #include <iostream> using namespace std; int main() { char*
I have the following code: #include <iostream> using namespace std; class testing{ int test()
I have the following code: #include stdafx.h #include <iostream> #include <conio.h> using namespace std;
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
I have following code #include <iostream> #include<exception> #include <cstdlib> int main(){ for (int i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.