Rewrite GoTo Statements

137 views Asked by At

I've been given the task of re-writing a program that uses goto statements for the sole purpose of demonstrating how frustrating they can be and I have learned that they are indeed very frustrating. After several hours of flipping through the debugger I am still running into problems, is there anyone who could pseudo code this for me that I could potentially understand it better? Apart from creating if and else statements for each goto I am lost.

namespace Assignment_03_Nasty_Code
{
    class Assignment03
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Assignment 03 = " + new Assignment03().Solve());
        }

        public int Solve() 
        {
            int result = 0;
            int upperSearchLimit = 1_000_000; 
            Console.WriteLine("Building list of primes...");
            ArrayList primes = Utils.BuildListOfPrimes(upperSearchLimit);
            Console.WriteLine(" Done.");
            // Step through the odd composite numbers
            for (int i = 19; i < upperSearchLimit; i+= 2) 
            { 
                Label02:
                if (primes.Contains(i)) 
                    goto Label03;
                // Is the number divisible by a prime?
                int j = 0;
                Boolean match = false;
                Label01:
                int tmp;
                int prime = (int)primes[j];
                tmp = i - prime;
                int half = tmp /= 2;
                int squareRoot = (int) Math.Sqrt(half);
                if (tmp != squareRoot * squareRoot) 
                    goto Label04;
                // We got one
                //System.out.println(i + " is a composite that can be written as the sum of a prime and twice a square");
                match = true;
                Console.WriteLine(i + " = " + (int)primes[j] + " + 2 * " + half );
                Label04: // Second goto
                j++;
                if ((int)primes[j] < i) 
                    goto Label01; 
                if (match == false) 
                {
                    Console.WriteLine("No match for " + i);
                    result = i; 
                    break;  
                }
                //goto Label02;
                Label03: // First goto
                int x;
            }
            return result;
        }
    }
}
1

There are 1 answers

5
RusArt On BEST ANSWER

I have replaced goto in your code and that's result

public int Solve()
{
    var result = 0;
    var upperSearchLimit = 1_000_000;
    Console.WriteLine("Building list of primes...");
    ArrayList primes = Utils.BuildListOfPrimes(upperSearchLimit);
    Console.WriteLine(" Done.");

    for (var i = 19; i < upperSearchLimit; i += 2)
    {
        if (primes.Contains(i))
        {
            continue;
        }

        var j = 0;
        var match = false;
        do
        {
            int tmp;
            var prime = (int)primes[j];
            tmp = i - prime;
            var half = tmp /= 2;
            var squareRoot = (int)Math.Sqrt(half);
            if (tmp == squareRoot * squareRoot)
            {
                match = true;
                Console.WriteLine(i + " = " + (int)primes[j] + " + 2 * " + half);
            }

            j++;
        } while ((int)primes[j] < i);

        if (match == false)
        {
            Console.WriteLine("No match for " + i);
            result = i;
            break;
        }
    }

    return result;
}