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
3 replies to this topic
#1
Posted 20 December 2011 - 03:54 PM
|
|
|
#2
Posted 20 December 2011 - 05:09 PM
Which language? Some languages don't have one or the other, and the details vary from one language to another.
#3
Posted 20 December 2011 - 06:13 PM
I don't think using those keywords is bad practice.
For C/C++, it's kind of like this:
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:
* * *
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 .
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
Posted 21 December 2011 - 07:19 AM
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


Sign In
Create Account

Back to top









