|
Hi Andrew, Thank you very much for the references. I see that this issue has already been discussed before and some people even patched Lua to get continue and even break N and continue N (I didn't want to go that far, I just wanted my little old "C" one-level continue). Of course one can use workarounds for anything, but if you don't give me continue I'll stick to conditional blocks, because IMO the other workarounds (special for functions, coroutines, tail calls, etc.) are too complex and syntactically contrived for the purpose. In my personal experience I have found many cases where continue is very handy. If you have several points in a long loop body where you would like to test a condition and eventually end the current iteration and you are forced to use a conditional block, you introduce one additional level of indentation at each decision point. This can be very annoying: while something do bla bla if contition1 then ble ble if condition2 then bli bli end end end Using continue stramlines the loop and IMO makes it much more readable: while something do bla bla if not contition1 then continue end ble ble if not condition2 then continue end bli bli end And this loop body has only two decision points! In practice it frequently happens to have more. IMO the conditional-block version suffers from "Pascalness". Enforcing a nested control structure does not necessarily enhance the comprehensibility of the code. On the contrary, many times the code becomes clearer when you can streamline the main flow of control (bla bla ble ble bli bli) and eventually depart from it when some condition is not met. The Pascal paradigm was that every block of code should have a single entry point and a single exit point. The first idea was very good and caught: no reasonable programming language today allows you to jump into the middle of a block. The second idea is wrong. See Khernigan's article, "Why Pascal is not my favorite programming language". This does not mean that jumping anywhere from the middle of a block is OK. But practice has shown that it is good and useful to be able to abort the execution of the current block by jumping to the end of it, and even to abort the execution of some enclosing block by jumping to the end of that block. That is the whole idea of continue, break and return. When I think of a loop body I imagine three nested blocks: the body block representing the current iteration, surrounded by the loop block, whose code controls the iteration, surrounded by the currently executing function. Break lets you jump to the end of the loop. Return lets you jump (conceptually) to the end of the function. Continue lets you jump to the end of the current iteration. All too natural. Plus, continue does not introduce any new syntax, only semantics, since it is syntactically legal exactly in the same places where break is. Well, I guess all this has been said before and probably solved by the patches I've seen. I just wonder why it is not officially accepted into the language. Thanks again and best regards, Hugo Andrew Wilson escribió: Hi Hugo, |