tags:

views:

139

answers:

5

From Schaums C++ text

Removal of goto- says use a flag

code segment:

const int N2 = 5;
int i, j, k;

  for (i = 0; i < N2; i++)
  { for (j = 0; j < N2; j++)
    { for (k = 0; k < N2; k++)
       if (i + j + k > N2)
           goto esc;
       else
          cout <<  i + j + k << " ";
       cout << "* ";
    }
   esc: cout << "." << endl;
  }

The solution:

const int 5;
int i, j, k;
bool done = false;
  for (i = 0; i < N2; i++)
  { for (j = 0; j < N2 && !done; j++)
    { for (k = 0; k < N2 && !done; k++)
       if (i + j + k > N2)
           done true;
       else
          cout <<  i + j + k << " ";
       cout << "* ";
    }
   cout << "." << endl;
   done = false;
  }

The output of the structured solution does not produce the same result...as the goto one... I can't see the problem

  1. Also, what would be another way to eliminate the goto?- Could I not use a flag and just compliment the condition.

Thanks ...

+3  A: 
const int 5;
int i, j, k;
bool done = false;
  for (i = 0; i < N2; i++)
  { for (j = 0; j < N2 && !done; j++)
    { for (k = 0; k < N2 && !done; k++)
       if (i + j + k > N2)
           done = true;
       else
          cout <<  i + j + k << " ";
       if (!done) // <-- Add this line
          cout << "* ";
    }
   cout << "." << endl;
   done = false;
  }
Sean Bright
Why not just put braces in the else statement, around both cout lines? That seems much more in line with the original.
Reed Copsey
Except that the innermost for statement only applies to one statement, and the cout << "*" statement belongs in the j loop. Yes, the formatting is quite confusing.
David Thornley
Yeah - missed that in the formatting mess. Part of why I always wrap everything, and am used to that...
Reed Copsey
I agree formatting is less desired- when I try to program- I know better...
iwanttoprogram
A: 

Throw an exception.

Vova
Nesting try blocks in for loops doesn't seem like a good idea to me.
David Thornley
Exceptions should be reserved for "exceptional" situations, which we have no indication that this is.
Todd Gardner
A: 

You need to add condition:

if (!done) cout << "* ";
klew
+6  A: 

A great way to code this kind of loop escape functionality is a return; statement. Take:

const int N2 = 5;

void inner_loop(const int i) {
   for (int j = 0; j < N2; ++j)
   {
      for (int k = 0; k < N2; ++k)
      {
        if (i + j + k > N2)
           return;

        cout <<  i + j + k << " ";
      }
      cout << "* ";
   }
}


for (int i = 0; i < N2; ++i)
{
  inner_loop(i);
  cout << "." << endl;
}
Todd Gardner
+1. To make it even cleaner, lose the "else" and collapse the two cout statements into one.
John Pirie
I was thinking the couts could be merged at first, but it changes the meaning from the original code (took me awhile to get that). I've edited it (added some braces around the furthest in for) to make it more clear.
Todd Gardner
+1  A: 

First of all, your formatting is extremely difficult to read. This helps a lot:

const int N2 = 5;
int i, j, k;

for (i = 0; i < N2; i++)
{ 
  for (j = 0; j < N2; j++)
  { 
    for (k = 0; k < N2; k++)
    {
      if (i + j + k > N2)
         goto esc;
     else
        cout <<  i + j + k << " ";
    }
    cout << "* ";
  }
 esc: cout << "." << endl;
}

Now I don't know who this Schaum guy is, but he's wrong. goto is a prefectly legitimate statement to use in this case. It's about the only reason you should ever need one, though. Eliminating the goto gains you nothing. Now you have an extra variable, and each loop needs an additional branch and test.

I suggest you avoid his advice on this issue.

rlbond
There is always a way around using gotos like Todd Gardner in this instance, I'd advise always trying to find them. Code changes over time what may look like a reasonable goto now could become a problem later.
Patrick
I believe it's one of those paranoia that haunts teachers. They are afraid of spaghetti code, as they should be. And I agree, goto's should be avoided. But in this kind of case, it's not actually making the code less manageable, as I think the goto version is easier to read than the other one.
Earlz
So goto is bad but break and continue are ok?
rlbond
I always thought that a continue, break, and return are not quite a bad as a goto- half the power- if you may say...
iwanttoprogram
continue and break ARE goto. They just only go to one place. The only difference between this goto and a break, is that the break only goes up one level. But we want to go up two levels. So if you are saying goto is easier to be abused, then yes. But the use of goto above is exactly the same as break except it goes 2 levels instead of 1.
rlbond