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

  • Home
  • SEARCH
  • 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 3975078
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:38:33+00:00 2026-05-20T04:38:33+00:00

I have been given an assignment in my CS class to come up with

  • 0

I have been given an assignment in my CS class to come up with a situation when it would be a good idea to use a goto statement and write a program for it. It can be any situation (though I have to write the program in 3 days) and so far I haven’t thought of anything that couldn’t easily be solved either with recursion or iteration. The only thing that I thought of was Towers of Hanoi, but that can be done in only a few lines with recursion. Do you have any suggestions? Thank you for your help.

EDIT: It has to be the actual goto statement, not other statements that serve the same purpose. For example, the user who answered by suggesting switch, raise, throw, etc, don’t work for the assignment.

  • 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-20T04:38:34+00:00Added an answer on May 20, 2026 at 4:38 am

    This C++ example is arguable:

    if(a)
    {
        ... // case 1
    }
    else if(b)
    {
        if(c)
        {
            ... // case 1 again
        }
        else
        {
            ... // case 2
        }
    }
    else
    {
        ... // case 3
    }
    

    The problem here is that you must either duplicate the code in the if() for case 1, or you must duplicate the code for handling case 1. In that example, the handling code was duplicated. See this alternate no-goto solution:

    if(a || (b && c))
    {
        ... // case 1
    }
    else if(b && !c)
    {
        ... // case 2
    }
    else
    {
        ... // case 3
    }
    

    Seems fine, except when the duplicated code is something very complicated. Then you start pulling things out into intermediate variables… moving logic away from the place where it’s demanded.

    Here’s a solution using goto which doesn’t have compromises:

    if(a)
    {
    case1:
        ... // case 1
    }
    else if(b)
    {
        if(c)
        {
            goto case1;// case 1 again
        }
        else
        {
            ... // case 2
        }
    }
    else
    {
        ... // case 3
    }
    

    In C++, scope is something to consider. goto in C++ is about the only way of jumping around execution with liberal regard to scope.

    Even if arguable, I still would not use goto in the sample I gave, because it complicates the order of execution. Knowing the state of things at case1: is really not very intuitive.

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

Sidebar

Related Questions

Recently for a programming class, we were given the assignment to write a program
I have an assignment in my C programming class to write a program to
For class I have an assignment: Write a C++ program that will output the
I have been given a requirement where I need to support multiple databases in
I have been given two different Microsoft Word document that my virus scanner has
I have been given a DLL (InfoLookup.dll) that internally allocates structures and returns pointers
I am given a problem where I have been given N nodes in a
Have recently been given a project to complete which uses XML quite extensively.Am looking
I have recently been given a task to add the ability to interact with
I have an assignment due in my intro database class - and I'm stumped

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.