FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


break statement





carlospro7
Is it good coding practice to use the break statement in an if or if else statement?
I know that they are essential when using them in a switch statement.
however when it comes to if, if else, I remember a teacher saying that it's bad practice, but I feel like that it is rather useful. what do you think?
qscomputing
I'm really not sure what you're asking. Assuming a C-like language, the switch statement looks something like this:

Code:
switch (condition) {
   case1: code; break;
   case2: code; break;
   case3: code; break;
   case4: code; /* FALL THROUGH */
   case5: code; break;
   default: code;
}


The switch allows you to choose between entry points into a block; the break statement is there to leave the block.

An if statement, OTOH, looks like this:

Code:
if (condition) {
   code;
   code;
} else {
   code;
   code;
}


A break would be completely redundant because an if is used to select between one of two blocks - not to jump to different parts in the same block.

Perhaps you could post some sample code so we can see where you put break in your if statements, because I'm somewhat confused as to why you would want one.
carlospro7
Code:

  while (!in_stream1.eof() || !in_stream2.eof())
  {
    //check for end of file in stream 2
    if (in_stream2.eof()) {
      out_stream << cur_num1 << " ";
      in_stream1 >> cur_num1;
    } else {
      if ((cur_num1 <= cur_num2) && (!in_stream1.eof())) {
          out_stream << cur_num1 << " ";
          in_stream1 >> cur_num1;
      }
    }
    //check for end of file in stream 1
    if (in_stream1.eof()) {                             //Line1
      if (in_stream2.eof()) break;                      //Line2 Break statement here
      out_stream << cur_num2 << " - ";
      in_stream2 >> cur_num2;
    } else {
      if ((cur_num2 <= cur_num1) && (!in_stream2.eof())) {
          out_stream << cur_num2 << " ";
          in_stream2 >> cur_num2;
      }
    }
  }


The reason I put a break statement is because w/o it the program printed an extra number that wasn't necessary, and it happened under a certain circumstance.

Anyway, I tried something else and I discovered that this also worked in the same way:

Code:

    ...
    //check for end of file for stream 1
    if (in_stream1.eof() && !in_stream2.eof()) {          //Line1 - Altered
      if (in_stream2.eof()) break;                        //Line2 - DELETED
      out_stream << cur_num2 << " - ";
    ...


My question simply was if break statements in if or if else statements were bad coding practice, but I think you made it clear that it is redundant, even though the compiler I used accepts it w/o any warnings. Thanks for the reply.
qscomputing
Yes, that second snippet you gave is pretty much the normal way of doing things. HTH.
Related topics
How To : Improve Your PHP Programming
Tutorial: Image Rollovers w/ Javascript
Echoing Titles
MySQL statement error (got me going)
was Hitler a Christian?
PHP OOP Tutorial
Justification for War in Iraq
break dance
An Example to mySQL class
F. Nietzsche
More front page news NOT on the front page..
Stanley "Tookie" Williams
US-Mexico border wall would be 'disgraceful': Fox
science vs. religion
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.