   Are break statements "wrong" or a bad habit?
  Seriously, my computer math teacher is pissing me off. Apparently break statements are wrong, when it's the only way that I can make this program work correctly: #include <iostream>#include <stdlib.h>#include <math.h>using namespace std;int main(int argc, char *argv[]){  bool divisorFound = false;  cout << "Enter a positive number: ";  int x;  cin >> x;  cout << "\n\n";for (int y = 1; y <= x; y += 2) // y increases until it is equal to x{   for (int z = 3; z < y; z += 2) // z increases until it is equal to y   { if (y % z == 0) // not prime {  divisorFound = true; // if dat shit ain't prime...    break; // get da fuck outta dat loop, bitch }   } if (divisorFound)  // if it isn't prime,    divisorFound = false; // reset divisor found else // if it is prime    cout << y << " "; // print the number}  system("PAUSE");  return 0;}Is there another way to do it without the break statement?   "Next time a terrorist gets freed after ten months of red tape, I’ll be sure to thank [the democrats]. I guarantee you that if someone is put in prison with the Patriot Act, even in the rare chance they are not a terrorist or have connections to terrorism, they are sure as hell guilty of something." -- A True Die-Hard American Patriot  [edited by - Mscgamer on February 18, 2004 3:00:07 PM]
int z = 3;while(z <= y && y % z)    z += 2;divisorFound = !(y % z)Though, personally, I find nothing wrong with break statements. If he doesn't like it, replace it with a goto. Or go read the 1968   (!) Communications of the ACM article that started the controversy: "Goto Statement Considered Harmful" by the late Edsger Dijkstra. Thenyou can argue. Opinions have evolved since that time, maybe your math teacher is obsolete and should be upgraded It was such a controversy that the ACM now has an (unwritten?) rule to reject any article of the form "X considered harmful".“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 18, 2004 11:13:26 AM];  I don't have problems with the break method, I don't know if there is much overhead but it's convienent none the less.Well you could do it this way for that inside loop. bool loopCheck = true;int z = 3;while(loopCheck){  if(z >= y)    loopCheck = false;  if(y % z == 0)  {    divisorFound = true;    loopCheck = false;  }z += 2} *edit* His is a better method.  Pumpkin Pieman - [Blogger]  Go to war again, blood is freedom's stain, But don't you pray for my soul anymore. [edited by - PumpkinPieman on February 18, 2004 11:17:02 AM];  so the TA''s in my C lab aren''t the only ones who hate break, heh.  I used it once (don''t even remember why) and the TA went on a tirade lecturing the entire lab on not being "High School Hackers" and to learn to code properly. ;  If you are writing a switch statement there is no avoiding them...   [ DGDev -- The Delphi Games Development Community ] [ Help GameDev.net fight cancer ];quote:If you are writing a switch statement there is no avoiding them...Seriously.I once had a professor who was of a similar opinion; he (as well as his clutch of TAs) insisted that one should never control loops with breaks or the continue statement, never "return" unless it was the last line line of a function, and a handful of other close-minded little mantras.Personally, if there is one thing I have learned in all my years of programming, is that the only valid "never use" rule is "never use any rule that says to never use anything." Everything has a place where it could be useful and/or elegant.There are an unfortunate handful of CS professors who are severely outdated and egotistical about it (like mine was), which is unfortunate because they pass on their biased mindset to their TAs and students. It''s a pity. ;  Some people say some pretty wierd things. "Don''t use break!" "Don''t use switch!" "Don''t use more then one return in each function!" "It''s more right to use .getBlaha!" and so on. They are only afraid. With some consideration, you can use whatever you want. ;  Use a break!!!IMHO thats actually a good example of when a break statement is actually a positive and elegant way of coding!Teachers seem to get wrapped up in their own opinions and methods of coding, I had university lecturers who insisted goto was the route of all evil...which is rediculous.Just as JPetrie stated, everything has a purpose. I''ve learnt my most valuable programming skills in the work place at the games company I work for and although some will scoff at the age of it I frequently use K&R "The C Programming Language" book which states and frequently uses the break to exit for loops.It''s perfectly legitimate and if it''s good enough for the designers of the language it''s good enough for me! :D ;o)GCoder ;  Just an additional point to show break statements aren't inherently wrong. In python you can add an else clause to for and while loops, which is only executed if the loop was not   interrupted by a break.x = int( raw_input( "Enter a positive number: " ) )for y in range(1,x+1,2):  for z in range(3,y,2):    if y % z == 0:  break  else:    print y,   Note - I believe you want z<y as your loop condition, not z<=y, since if z==y, then z%y==0. “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 18, 2004 12:38:49 PM];quote:Original post by Fruny  Note - I believe you want z<y as your loop condition, not z<=y, since if z==y, then z%y==0.That would kinda break the prime test I guess 