Jump to content

Using break and continue

- - - - -

  • Please log in to reply
3 replies to this topic

#1
ksimonsand

ksimonsand

    Newbie

  • Members
  • Pip
  • 2 posts
Hello

I have started with programming recently and there are a couple of things which I find rather confusing. The one I would like to ask about is the use of "break" and "continue" inside loops. I was told using them is a bad practice and they should be avoided. Could anybody kindly explain what is wrong about them. They seem to get the job done!

Thanks in advance

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Which language? Some languages don't have one or the other, and the details vary from one language to another.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I don't think using those keywords is bad practice.

For C/C++, it's kind of like this:
x= -1; 

while (x < 4){ 

  x++; 

  printf ("nothing\r\n"); 

  if (x == 1) continue; 

  printf ("one\r\n"); 

  if (x == 2) break; 

  printf ("three\r\n"); 

} 

printf ("finished loop\r\n"); 

The code above should set x to 0. Then it would enter a block for a loop that exits when x is no longer less than 4. It would increment (add 1 to) x, making x be 0. It would print "nothing" followed by a new line. x is not 1 yet, so it would skip whatever's after the 'if' parentheses (do nothing). Then print "one" followed by a new line. x is not 2, so operating as usual. Output "three" followed by a new line. The instruction pointer or program counter goes back to the beginning of the block (where it says to print "nothing").

Increment x, making it 1. Print "nothing" followed by a new line. If x is 1, which it at this moment is, then continue, or in other words jump back to the start of the block. The instruction pointer (or program counter) is now pointing at where the curly bracket starts.

Increment x, so it's now 2. Print "nothing" followed by a new line. x is not 1, so do nothing. Print "one" followed by a new line. If x is 2, which it currently is, then break from the loop, or in other words jump forward to the end of the block. The instruction pointer is now pointing at where the curly bracket ends.

Print "finished loop" followed by a new line.

So basically, in a loop, the continue keyword makes the execution of the program jump to the start of the loop block, while the break keyword makes the execution of the program jump to the end of the loop block.

Also, in 'for' loops, such as this:
for (statement1; statement2; statement3) code... 
, the continue keyword executes statement3, as well as jumping back to the start of the loop block.

* * *

As for other languages, a lot of them have at least something similar. For example, JavaScript has the same keywords under the same names. From what I can recall at the moment, the Perl notion for 'continue' and 'break' is 'next' and 'last', respectively. In Ruby, the same would be 'next' and 'break', instead of 'continue' and 'break', in that order.




I hope this helps :D .

#4
ksimonsand

ksimonsand

    Newbie

  • Members
  • Pip
  • 2 posts

WingedPanther said:

Which language? Some languages don't have one or the other, and the details vary from one language to another.

I am using C#.[COLOR="Silver"]

---------- Post added at 03:19 PM ---------- Previous post was at 03:18 PM ----------

Thanks a lot for the explanation :) I appreciate your reply.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users