Help avoid goto

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • spasmous

    Help avoid goto

    // return early if all points are the same
    for(int i=1; i<n; i++)
    if(y[i] != y[0]) goto SKIP;
    return;
    SKIP:

    Can someone help me with an alternative to this snippet that avoids
    goto? Without introducing a new variable?
  • Walter Roberson

    #2
    Re: Help avoid goto

    In article <fcbdd75e-5760-48c3-8bf3-4da28bd084b4@x1 9g2000prg.googl egroups.com>,
    spasmous <spasmous@gmail .comwrote:
    >// return early if all points are the same
    >for(int i=1; i<n; i++)
    if(y[i] != y[0]) goto SKIP;
    >return;
    >SKIP:
    >Can someone help me with an alternative to this snippet that avoids
    >goto? Without introducing a new variable?
    int i = 1;
    while( i<n && y[i] != y[0] ) i++;
    if (i == n) return;

    This can also be written as a for loop.
    --
    "Let me live in my house by the side of the road --
    It's here the race of men go by.
    They are good, they are bad, they are weak, they are strong
    Wise, foolish -- so am I;" -- Sam Walter Foss

    Comment

    • pete

      #3
      Re: Help avoid goto

      spasmous wrote:
      // return early if all points are the same
      for(int i=1; i<n; i++)
      if(y[i] != y[0]) goto SKIP;
      return;
      SKIP:
      >
      Can someone help me with an alternative to this snippet that avoids
      goto? Without introducing a new variable?
      int i;

      for (i = 1; n i; ++i) {
      if (y[i] != y[0]) {
      break;
      }
      }
      if (i == n || 1 n) {
      return;
      }


      --
      pete

      Comment

      • Ben Bacarisse

        #4
        Re: Help avoid goto

        spasmous <spasmous@gmail .comwrites:
        // return early if all points are the same
        for(int i=1; i<n; i++)
        if(y[i] != y[0]) goto SKIP;
        return;
        SKIP:
        >
        Can someone help me with an alternative to this snippet that avoids
        goto? Without introducing a new variable?
        9 times out of 10, this sort of situation is a reminder that you need
        another function. Testing if an array has all elements equal is the
        job of a separate function. There are lots of ways to write it:

        bool all_equal(T *array, size_t n)
        {
        for (size_t i = 1; i < n; i++)
        if (array[i] != array[0])
        return false;
        return true;
        }

        May people prefer this style (I think I do):

        bool all_equal(T *array, size_t n)
        {
        for (size_t i = 1; i < n && array[y] == array[0]; i++)
        continue;
        return i == n;
        }

        (Note that they are not the same when n == 0 -- you need to decide what
        you mean by that case.)

        I had to use T because I don't know the type of the elements of y. If
        you don't like using C99isms, move the declaration of i out of the
        loop and return int rather than bool (or declare bool yourself).

        --
        Ben.

        Comment

        • spasmous

          #5
          Re: Help avoid goto

          On Jun 20, 12:28 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
          wrote:
          >
          int i = 1;
          while( i<n && y[i] != y[0] ) i++;
          if (i == n) return;
          >
          This can also be written as a for loop.
          Very nice Walter. I went with a for loop variant.

          // return early if all points are the same
          for(int i=1; y[i]!=y[0]; i++)
          if(i == n) return;





          Comment

          • Eric Sosman

            #6
            Re: Help avoid goto

            spasmous wrote:
            On Jun 20, 12:28 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
            wrote:
            >int i = 1;
            >while( i<n && y[i] != y[0] ) i++;
            >if (i == n) return;
            >>
            >This can also be written as a for loop.
            >
            Very nice Walter. I went with a for loop variant.
            >
            // return early if all points are the same
            for(int i=1; y[i]!=y[0]; i++)
            if(i == n) return;
            Note that this tests y[n], the (n+1)st array element.

            --
            Eric.Sosman@sun .com

            Comment

            • Walter Roberson

              #7
              Re: Help avoid goto

              In article <10adaa4c-660f-4981-a425-e0b562002190@s3 3g2000pri.googl egroups.com>,
              spasmous <spasmous@gmail .comwrote:
              >On Jun 20, 12:28=A0pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
              >wrote:
              >>
              >int i = 1;
              >while( i<n && y[i] != y[0] ) i++;
              >if (i == n) return;
              >>
              >This can also be written as a for loop.
              >
              >Very nice Walter. I went with a for loop variant.
              >
              >// return early if all points are the same
              >for(int i=1; y[i]!=y[0]; i++)
              if(i == n) return;
              If y is defined from index 0 to n-1 then your code will access
              y[n] in the termination test, which would be undefined behaviour
              under that sizing assumption.

              --
              "When a scientist is ahead of his times, it is often through
              misunderstandin g of current, rather than intuition of future truth.
              In science there is never any error so gross that it won't one day,
              from some perspective, appear prophetic." -- Jean Rostand

              Comment

              • Kaz Kylheku

                #8
                Re: Help avoid goto

                On Jun 20, 12:19 pm, spasmous <spasm...@gmail .comwrote:
                // return early if all points are the same
                for(int i=1; i<n; i++)
                    if(y[i] != y[0]) goto SKIP;
                return;
                SKIP:
                >
                Can someone help me with an alternative to this snippet that avoids
                goto? Without introducing a new variable?
                {
                // ...
                for (int i = 1; i < n; i++) {
                if (y[i] != y[0]) {
                // entire ``SKIP:'' section here
                break;
                }
                }
                }

                Like, put the conditional code into the body of the conditional
                statement which tests that condition? Doh?

                The reason you have a goto is that you relocated that logic away from
                that construct. In general, you can't arbitrarily relocate control
                without using GOTO or additional state variables.

                Comment

                • Kaz Kylheku

                  #9
                  Re: Help avoid goto

                  On Jun 20, 1:12 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
                  9 times out of 10, this sort of situation is a reminder that you need
                  another function.  Testing if an array has all elements equal is the
                  job of a separate function.  There are lots of ways to write it:
                  >
                  bool all_equal(T *array, size_t n)
                  {
                      for (size_t i = 1; i < n; i++)
                          if (array[i] != array[0])
                              return false;
                      return true;
                  >
                  }
                  >
                  May people prefer this style (I think I do):
                  >
                  bool all_equal(T *array, size_t n)
                  {
                      for (size_t i = 1; i < n && array[y] == array[0]; i++)
                          continue;
                      return i == n;

                  This algorithm is not the same as the first version with the early
                  return. What if the array size is zero? The ``all equal'' condition is
                  true then: all elements of an empty sequence are equal to each other,
                  because it is not the case that there exist two distinct positions x
                  and y such that the x-th element is equal to the y-th element.

                  Moreover, the variable i is not in scope of the i == n expression.

                  Comment

                  • Kaz Kylheku

                    #10
                    Re: Help avoid goto

                    On Jun 20, 1:12 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
                    bool all_equal(T *array, size_t n)
                    {
                        for (size_t i = 1; i < n && array[y] == array[0]; i++)
                            continue;
                        return i == n;
                    The name i is not in scope here, oops! :)
                    }
                    >
                    (Note that they are not the same when n == 0 -- you need to decide what
                    you mean by that case.)
                    That case means that all elements are equal. For an empty sequence, it
                    is not the case that there can exist two distinct indices x and y such
                    that the x-th element is equal to the y-th element. Since you can't
                    find two unequal elements, you cannot declare that the sequence does
                    not have all elements equal.

                    Here is another view. The all-equal property, if true of a sequence S,
                    should also be true of a subsequence of S, because how could it be the
                    case that all elements of a large sequence are equal, yet elements of
                    a subsequence of that sequence are not equal? But the empty sequence E
                    is a subsequence of every sequence; if the property is declared not
                    true of E by definition, then this represents a troublesome special
                    case; there now exists a subsequence of all-equal sequence S for which
                    the property does not hold.

                    Comment

                    • Ben Bacarisse

                      #11
                      Re: Help avoid goto

                      Kaz Kylheku <kkylheku@gmail .comwrites:
                      On Jun 20, 1:12 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
                      >9 times out of 10, this sort of situation is a reminder that you need
                      >another function.  Testing if an array has all elements equal is the
                      >job of a separate function.  There are lots of ways to write it:
                      >>
                      >bool all_equal(T *array, size_t n)
                      >{
                      >    for (size_t i = 1; i < n; i++)
                      >        if (array[i] != array[0])
                      >            return false;
                      >    return true;
                      >>
                      >}
                      >>
                      >May people prefer this style (I think I do):
                      >>
                      >bool all_equal(T *array, size_t n)
                      >{
                      >    for (size_t i = 1; i < n && array[y] == array[0]; i++)
                      >        continue;
                      >    return i == n;
                      >
                      >
                      This algorithm is not the same as the first version with the early
                      return. What if the array size is zero?
                      I know. Why did you clip the bit where I said that? I explained they
                      were not the same and when.
                      Moreover, the variable i is not in scope of the i == n expression.
                      That is a good point. Thank you.

                      --
                      Ben.

                      Comment

                      • Daniel Pitts

                        #12
                        Re: Help avoid goto

                        spasmous wrote:
                        // return early if all points are the same
                        for(int i=1; i<n; i++)
                        if(y[i] != y[0]) goto SKIP;
                        return;
                        SKIP:
                        >
                        Can someone help me with an alternative to this snippet that avoids
                        goto? Without introducing a new variable?
                        if (isAllEqual(y, n)) return;
                        ....
                        int isAllEqual(foo *y, int n) {
                        for (int i = 0; i <n; ++i) {
                        if (y[n] != y[0]) {
                        return false;
                        }
                        }
                        return true;
                        }

                        --
                        Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>

                        Comment

                        • Mike Wahler

                          #13
                          Re: Help avoid goto


                          "spasmous" <spasmous@gmail .comwrote in message
                          news:fcbdd75e-5760-48c3-8bf3-4da28bd084b4@x1 9g2000prg.googl egroups.com...
                          // return early if all points are the same
                          for(int i=1; i<n; i++)
                          if(y[i] != y[0]) goto SKIP;
                          if(y[i] != y[0])
                          break;


                          -Mike
                          return;
                          SKIP:
                          >
                          Can someone help me with an alternative to this snippet that avoids
                          goto? Without introducing a new variable?

                          Comment

                          • CBFalconer

                            #14
                            Re: Help avoid goto

                            spasmous wrote:
                            >
                            // return early if all points are the same
                            for(int i=1; i<n; i++)
                            if(y[i] != y[0]) goto SKIP;
                            return;
                            SKIP:
                            >
                            Can someone help me with an alternative to this snippet that
                            avoids goto? Without introducing a new variable?
                            for (i = 1; i < n; i++)
                            if (y[i] != y[0] break;
                            if (n == i) return;
                            else {
                            /* SKIP is here */
                            }

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.

                            ** Posted from http://www.teranews.com **

                            Comment

                            • Ben Bacarisse

                              #15
                              Re: Help avoid goto

                              Kaz Kylheku <kkylheku@gmail .comwrites:
                              On Jun 20, 1:12 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
                              >bool all_equal(T *array, size_t n)
                              >{
                              >    for (size_t i = 1; i < n && array[y] == array[0]; i++)
                              >        continue;
                              >    return i == n;
                              >
                              The name i is not in scope here, oops! :)
                              Yes, you said that already. Do you get paid for posts? :-)
                              >}
                              >>
                              >(Note that they are not the same when n == 0 -- you need to decide what
                              >you mean by that case.)
                              >
                              That case means that all elements are equal.
                              Yes.
                              For an empty sequence, it
                              is not the case that there can exist two distinct indices x and y such
                              that the x-th element is equal to the y-th element. Since you can't
                              find two unequal elements, you cannot declare that the sequence does
                              not have all elements equal.
                              I agree that there is only one logical return value for the empty
                              case, but I opted just to suggest that the OP think it over.

                              --
                              Ben.

                              Comment

                              Working...