Re: Advocacy for a change for the continue key word in while entry loops.

new topic     » goto parent     » topic index » view thread      » older message » newer message
SDPringle said...

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.

(snip)

What do you think?

I'm inclined to agree however I'm unsure if this is the correct solution. I think the entry feature is great and I use it occasionally, but I also think consistency is important and we should remain predictable compared to other languages.

I've perused each of these below and they're all consistent with each other: continue returns to the top of the loop. However, none of them have an "entry clause" like we do, so we may be in uncharted territory here.

C: https://en.cppreference.com/w/c/language/while
C#: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/statements#1392-the-while-statement
C++: https://en.cppreference.com/w/cpp/language/while
Go: https://go.dev/ref/spec#For_condition
Java: https://docs.oracle.com/javase/specs/jls/se23/html/jls-14.html#jls-14.12
JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
Lua: https://www.lua.org/manual/5.4/manual.html#3.3.4
Pascal: https://docwiki.embarcadero.com/RADStudio/Athens/en/Declarations_and_Statements_(Delphi)#Control_Loops
PHP: https://www.php.net/manual/en/control-structures.while.php
Python: https://docs.python.org/3/reference/compound_stmts.html#the-while-statement
Ruby: https://docs.ruby-lang.org/en/3.4/syntax/control_expressions_rdoc.html#label-while+Loop
Rust: https://doc.rust-lang.org/reference/expressions/loop-expr.html
Swift: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow#While-Loops
Visual Basic: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/while-end-while-statement

Technically you can write entry and label on the same line, so this code is valid and IMHO a relatively clean way to solve the problem you're describing.

include std/io.e 
object line 
while sequence(line) with entry do 
    if length(line) = 0 then 
        goto "entry" 
    end if 
entry label "entry" 
    line = gets(STDIN) 
end while 

I'm leaning toward allowing the syntax goto entry as a solution. This would keep continue unchanged, so its behavior remains consistent to when the entry clause is not in play. It's basically shorthand for the example above, and I think the phrase "goto entry" literally speaks for itself.

include std/io.e 
object line 
while sequence(line) with entry do 
    if length(line) = 0 then 
        goto entry 
    end if 
entry 
    line = gets(STDIN) 
end while 

While we're on the topic, another thing that's been musing in the back of my mind lately is allowing the while condition to see within the scope of its loop. If a for loop scopes its variable local only to the loop, why can't a while loop do the same? I'm inclined to require that such variables must be declared at the top of the loop to remain consistent with variable scoping elsewhere.

So this would be legal:

include std/io.e 
while sequence(line) with entry do 
    object line 
    if length(line) = 0 then 
        goto entry 
    end if 
entry 
    line = gets(STDIN) 
end while 

But this would not:

include std/io.e 
while sequence(line) with entry do 
    if length(line) = 0 then 
        goto entry 
    end if 
entry 
    object line = gets(STDIN) 
end while 

I'm entirely open to input on all of this.

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu