Advocacy for a change for the continue key word in while entry loops.
- Posted by SDPringle 2 months ago
- 362 views
Anyone with experience with while-entry loops either has opted to use goto when you would have wanted to use continue or just has somehow avoided the coding pattern of re-iterating loops. I advocate that the continue key word ought to jump the execution to the entry clause of the while statement rather than the check condition part. The entry clause is where the next line of the data is read.
include std/io.e object line while atom(line) do if equal(line,"") then -- I want to skip the rest of the code and go to the next line here. -- no such thing in Euphoria. end if entry line = gets(STDIN) end while
The jumping to the entry clause is natural because if you convert a for loop into a while entry loop, continue as it is now does the wrong thing.
-- The following code finds the primes smaller than 10: sequence primes = {0, 2, 3, 4, 5, 6, 7, 8, 9} for i = 1 to 9 do if primes[i] = 0 then continue end if for j = 2*primes[i] to 9 by primes[i] do primes[j] = 0 end for end for ? primes
In converting the above into a while-entry loop, one must change out continue for label/goto or you will get an infinite loop bug
sequence primes = {0, 2, 3, 4, 5, 6, 7, 8, 9} integer i = 1 while i <= 9 with entry do if primes[i] = 0 then -- bug continue end if for j = 2*primes[i] to 9 by primes[i] do primes[j] = 0 end for entry i += 1 end while ? primes ? primes
While-entry loops do not have any use of continue, and if they do they should be renamed to redo because you are not going to the next iteration but simply doing the same iteration over again. In order to correctly convert the first code to use a while - entry loop you must use goto/label like this:
-- The conversion to a while loop one must get rid of -- continue and use goto. This will infinite loop: sequence primes = {0, 2, 3, 4, 5, 6, 7, 8, 9} integer i = 1 while i <= 9 with entry do if primes[i] = 0 then goto "Increment" end if for j = 2*primes[i] to 9 by primes[i] do primes[j] = 0 end for entry label "Increment" i += 1 end while ? primes
What do you think?