Feature Request: Short Syntax For Empty While Loop
See original GitHub issueI’d like to write while (!(this.done && other.done));
instead of while (!(this.done && other.done)) {}
for non-blocking threaded code, where the while
loop is often used to wait for a value to compute and doesn’t actually contain anything besides the head. This would simply be for more code clarity, nothing else.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Loops: while and for - The Modern JavaScript Tutorial
The while loop has the following syntax: while (condition) { // code ... For instance, a shorter way to write while (i !=...
Read more >Python syntax for an empty while loop - Stack Overflow
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. http://www.
Read more >do...while - JavaScript - MDN Web Docs - Mozilla
A statement that is executed at least once and is re-executed each time the condition evaluates to true. To execute multiple statements within...
Read more >JavaScript Loops Explained: For Loop, While Loop, Do...while ...
Syntax. The while loop starts by evaluating condition . If condition evaluates to true , the code in the code block gets executed....
Read more >Python "while" Loops (Indefinite Iteration)
pop() method and the list is empty, a is false, and the loop terminates. Remove ads. The Python break and continue Statements. In...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I feel like it’s much clearer that
while
is waiting for something to happen instead of making it happen itself when the curly braces are actually there. I’d even put them in their own line:If you didn’t compare and set the value in a single operation or using a lock, the condition can succeed, then the scheduler could switch the context, that other context could do something that’d cause the condition to return false, then the scheduler switches back to the previous context and executes code while the condition is actually false and not true, which may lead to unwanted behaviour.
To prevent race conditions you either need to lock parts of code or you use atomic operations. Atomic operations reflect CPU commands, so they don’t need explicit locks. Either the whole operation fails or it succeeds, that’s why you work with a while loop, trying to execute the atomic operation as long as needed for it to succeed. In best case, the operation only needs a single try, at which point it’s much faster than always locking and unlocking specific parts of the code.
As this is completely dependant on one’s opinion, that’s the exact reason why I’m trying to convince people through examples. That’s how low-level programming languages evolved into high-level programming languages. People repeatedly wrote code, that may as well be abbreviated through some kind of construct to simplify code writing and/or reduce the chance of producing errors.
Personally, I’d like to have this shorter syntax, but if that feature is deemed unnecessary by the majority, then so be it. 😃