1. break bug
- Posted by bryanso Nov 08, 2012
- 1423 views
Why doesn't this break command break out of the "for i" loop?
procedure f() for i = 1 to 5 do printf(1, "i is %d\n", i) if i = 3 then for j = 1 to 5 do printf(1, " j is %d\n", j) end for break end if end for end procedure f()
Result:
i is 1 i is 2 i is 3 j is 1 j is 2 j is 3 j is 4 j is 5 i is 4 i is 5
2. Re: break bug
- Posted by bryanso Nov 08, 2012
- 1442 views
Sorry... should have used exit!
Ignore me.
3. Re: break bug
- Posted by useless_ Nov 08, 2012
- 1416 views
Sorry... should have used exit!
Ignore me.
Better yet, goto would have been even cleaner!
useless
4. Re: break bug
- Posted by bryanso Nov 08, 2012
- 1385 views
Sorry... should have used exit!
Ignore me.
Better yet, goto would have been even cleaner!
useless
Interesting post modern thinking. Like it.
5. Re: break bug
- Posted by bryanso Nov 08, 2012
- 1345 views
The introduction of break statement, causing a confusion between break and exit, seems to violate the principle of "say no to complicated programming".
6. Re: break bug
- Posted by mattlewis (admin) Nov 08, 2012
- 1369 views
The introduction of break statement, causing a confusion between break and exit, seems to violate the principle of "say no to complicated programming".
It's a little bit subtle. Basically, exit gets you out of loops (for, while, loop) and break gets you out of conditionals (if, switch).
Matt
7. Re: break bug
- Posted by ghaberek (admin) Nov 08, 2012
- 1357 views
The introduction of break statement, causing a confusion between break and exit, seems to violate the principle of "say no to complicated programming".
I suppose this started because we only had exit for the longest time. And only with the recent addition of switch did we gain break.
-Greg
8. Re: break bug
- Posted by jaygade Nov 09, 2012
- 1353 views
But goto is so much cleaner. Why use two words when one will do?
It's like Hodor in Game of Thrones. Whatever the question, the answer is always "Hodor".
(Sarcasm)
9. Re: break bug
- Posted by jimcbrown (admin) Nov 09, 2012
- 1341 views
The introduction of break statement, causing a confusion between break and exit, seems to violate the principle of "say no to complicated programming".
I suppose this started because we only had exit for the longest time. And only with the recent addition of switch did we gain break.
-Greg
The reason we came up with a new keyword for switch, instead of simply reusing exit, is to allow the user to embed a switch statement inside of a loop but then use the exit keyword inside of the switch to break out of the loop.
E.g.
while still_running do integer k = get_key() switch k do case 'q' then exit end switch end while
10. Re: break bug
- Posted by useless_ Nov 09, 2012
- 1324 views
The introduction of break statement, causing a confusion between break and exit, seems to violate the principle of "say no to complicated programming".
I suppose this started because we only had exit for the longest time. And only with the recent addition of switch did we gain break.
-Greg
The reason we came up with a new keyword for switch, instead of simply reusing exit, is to allow the user to embed a switch statement inside of a loop but then use the exit keyword inside of the switch to break out of the loop.
But only if you use "with fallthru" keywords! Anything to make a goto but using different letters, eh?
switch x with fallthru case a: -- blah blah code case b: -- blah blah code i wanna leave here, was it exit or break? case c: -- blah blah code end switch
But imagine the horrors of not having "case", "switch", "exit", "break", and "with fallthru":
goto x :a -- blah blah code :b -- blah blah code ; goto :end_x :c -- blah blah code :end_x
useless
11. Re: break bug
- Posted by bryanso Nov 09, 2012
- 1310 views
What is the logical motivation or perhaps historical limitation for C to allow fall through in switch statements? It's not intuitive and is a cause of many accidental bugs (at least for me).
12. Re: break bug
- Posted by jimcbrown (admin) Nov 09, 2012
- 1302 views
The reason we came up with a new keyword for switch, instead of simply reusing exit, is to allow the user to embed a switch statement inside of a loop but then use the exit keyword inside of the switch to break out of the loop.
But only if you use "with fallthru" keywords!
No, using the exit keyword in this way works either way, with or without fallthru.
13. Re: break bug
- Posted by jaygade Nov 09, 2012
- 1286 views
What is the logical motivation or perhaps historical limitation for C to allow fall through in switch statements? It's not intuitive and is a cause of many accidental bugs (at least for me).
I think it has to do with the way the original C code was compiled to assembly/machine language.
I believe that a switch statement usually just compiles into a calculated jump/goto, and each case is just a piece of code that is jumped into. In older memory-constrained computers, you could save some code space by sharing common code between cases.
14. Re: break bug
- Posted by DerekParnell (admin) Nov 10, 2012
- 1214 views
Sorry... should have used exit!
Better yet, goto would have been even cleaner!
Well, that's one opinion
I think that idea of 'goto is evil' has got it wrong, because it's not the jump that is the problem but the label. What I mean is that when one sees goto SomeLabel in code, it's not so hard to follow where the control logic flows to. But when one sees label SomeLabel in code, there can be a lot of needless work to find out all the possible ways that a program can get to that label.
The main aim of both break and exit is to eliminate excessive labels. And even when you need a label on those statements, what Euphoria does is not to name the destination of a jump, but names the construct that one is trying to leave. This significantly reduces the scope of a jump to something that is manageable for a human reader of code.
Now, when it comes to the difference between break and exit, the development team had lots of heated discussion about both the need for two statements and also what those statements would be. The exit statement was a given, seeing that it was already well established in Euphoria as the way to prematurely leave a loop construct. To change this would cause too much existing code to fail.
With the introduction of the switch construct, when fallthru was in effect, we needed a way to allow some code to leave the switch rather than automatically fall through to the next statement. If we had of used exit, it would have caused ambiguity when a switch was inside a loop or visa versa - are we trying to leave the switch or leave the loop? We could have resolved this by using the exit label form but that could have resulted in a lot more typing and/or accidentally introduced bugs (when the label phrase was forgotten to be added).
Another idea was to use exit if and exit switch, but this might have made parsing some code a problem. (I can't see it myself but I was in the minority on that one).
The break statement was already in use by a few languages for this purpose, so it was decided to continue with this idea in Euphoria. The use of break should be very rare in Euphoria because fallthru is rare in switches and breaking out of a nested if statement is also a rare event.
15. Re: break bug
- Posted by jaygade Nov 10, 2012
- 1162 views
And you can always use "goto" if that is what you prefer. Heh, I would have figured that argument was dead now that Euphoria has "goto".