Skip to main content
GameDev.net gamedev.net
🔒 Locked

Are break statements "wrong" or a bad habit?

Started by Mscgamer Feb 18, 2004 at 10:00 AM 26 replies 5.1k views
Original Post
Mscgamer
Mscgamer
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]
Fruny
Fruny

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. Then you 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]
"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
PumpkinPieman
PumpkinPieman
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]
Signpost
Signpost
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.
jpetrie
jpetrie
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.
Jolle
Jolle
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.
GCoder
GCoder
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
GCoder
Fruny
Fruny
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]
"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
Shadowdancer
Shadowdancer
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
HairyTroll
HairyTroll
Here are a couple of amusing posts from Linus Torvalds on the use of GOTO in the Linux Kernel.

Reply #1

Reply #2

curtmax_0
curtmax_0
Actually, you CAN use a switch statement without break. But then it goes on to the next case.. which I have never found a use for. People that say continues and breaks are bad are full of bs.

In my opinion, it makes some code much cleaner and easier to read. I am against goto though... never found a use for it, however I don''t get mad at people that use it. (I figure they are BASIC people )
gregs
gregs
quote:
Original post by HairyTroll
Here are a couple of amusing posts from Linus Torvalds on the use of GOTO in the Linux Kernel.

Reply #1

Reply #2




I think I''d take Niklaus Wirth over Linus any day. All languages have their uses.
CorneoUCD
CorneoUCD
I don''t get why some professors tell their students not to use a break or continue statement. Why damnit!

I think, if you are using a switch then you will most likely need to use a break statement. Then again, I have only seen switch used for menu options.
Jolle
Jolle
quote:
Original post by curtmax_0 I am against goto though... never found a use for it, however I don''t get mad at people that use it. (I figure they are BASIC people )

Basic got while .. wend, do .. loop, for .. next and all that stuff, you know. So no need to use goto in most basic versions nowdays, no more than in c/c++ or whatever. Therefore: Bad joke
Shadowdancer
Shadowdancer
quote:
Original post by curtmax_0
In my opinion, it makes some code much cleaner and easier to read. I am against goto though... never found a use for it, however I don''t get mad at people that use it. (I figure they are BASIC people )


Or Perl people

No, seriously, as long as you don''t use gousub-esque convulsions in a language with a concept of functions and don''t jump between scopes, I don''t see too many problems.
Shadowdancer
Shadowdancer
quote:
Original post by Mscgamer
#include <iostream>
#include <stdlib.h>
#include <math.h>



BTW, using the <cstdlib> and <cmath> headers would probably be better (tailored to C++).

[edited by - Shadowdancer on February 18, 2004 2:01:13 PM]
Warpstorm
Warpstorm
quote:
Original post by CorneoUCD
I don''t get why some professors tell their students not to use a break or continue statement. Why damnit!



Because they are from the era of structured programming (when Pascal was still taught) where the rule was "One way in, one way out". This came about because of the "goto are considered harmful" fallout. By extension, anything that doesn''t fit the "One way" rule is bad.
doctorsixstring
doctorsixstring
break statements are not inherently wrong, but they can make for poor code when used incorrectly (as can pretty much anything else in a language). Mscgamer''s use of break to exit a for() loop if a poor use of break. Fruny''s while() loop is much cleaner.

The use of break in a C/C++ switch() block is something else entirely, as a few of you mentioned.

What I would like to know is if Mscgamer''s teacher issued a blanket statement "BREAKS ARE SATAN-SPAWN!", or a more reasonable "You should not use a break statement in a for() loop. A while() loop would be better."

-Mike
kspansel
kspansel
With the exception of the switch statement, you can ALWAYS re-write a portion of code using a break statement, into one that doesn't use it. Plus as, Warpstorm said, "one way in, one way out." It's good to know the state of your loop, or any piece of code for that matter, when when you exit.

It's all about using the proper type of loop anyway.

Kory

"The pioneers of a warless world are the youth who refuse military service" - Albert Einstein

[edited by - kspansel on February 18, 2004 2:47:03 PM]

[edited by - kspansel on February 18, 2004 2:47:26 PM]
"The pioneers of a warless world are the youth who refuse military service" - Albert Einstein

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.