什么时候可以使用GOTO

时间:2011-01-31 15:33:22

标签: goto

  

可能重复:
  GOTO still considered harmful?

什么时候可以使用GOTO?

例如,此代码是GOTO的可接受用途吗?为什么或为什么不呢?

        for (int y = 0; y < largest.Rectangle.Height; y++) {
            //the line is a goodie
            if (whitePixelCountPerLine[y] > threshold) {
                //g.DrawLine(new System.Drawing.Pen(Color.Blue, 1), largest.Rectangle.X, largest.Rectangle.Y + y, largest.Rectangle.X + largest.Rectangle.Width, largest.Rectangle.Y + y);
            } else {
                whitePixelCountPerLine[y] = 0;
                //check above it
                for (int i = y; i > 0 && i > y - distanceThreshold; i--) {
                    if (whitePixelCountPerLine[i] > threshold) {
                        //g.DrawLine(new System.Drawing.Pen(Color.Blue, 1), largest.Rectangle.X, largest.Rectangle.Y + y, largest.Rectangle.X + largest.Rectangle.Width, largest.Rectangle.Y + y);
                        whitePixelCountPerLine[y] = 1;
                        goto Endloop;   //if I break here, we still do the second loop, so a goto is the most readable way to skip
                    }
                }
                //check below it
                for (int i = y; i < largest.Rectangle.Height && i < y + distanceThreshold; i++) {
                    if (whitePixelCountPerLine[i] > threshold) {
                        //g.DrawLine(new System.Drawing.Pen(Color.Blue, 1), largest.Rectangle.X, largest.Rectangle.Y + y, largest.Rectangle.X + largest.Rectangle.Width, largest.Rectangle.Y + y);
                        whitePixelCountPerLine[y] = 1;
                        goto Endloop;   //keeping with the pattern
                    }
                }
            }
        Endloop:    //AH! A label for a GOTO, see the two goto statements above, they say why this is needed.
            continue;
        }

0 个答案:

没有答案