Thread overview | |||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 24, 2016 OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Hi all, In the source code, written in D, is often used in the design of the `for (;;) { ... }` Maybe someone has specific examples of translation of code in asm, where `while (true) { ... }` or `for (;;) { ... }` affect the performance or cross-platform programs. It would be interesting to see samples. |
November 24, 2016 Re: OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Thursday, 24 November 2016 at 21:57:15 UTC, Dennis Ritchie wrote: > Hi all, > > In the source code, written in D, is often used in the design of the `for (;;) { ... }` > > Maybe someone has specific examples of translation of code in asm, where `while (true) { ... }` or `for (;;) { ... }` affect the performance or cross-platform programs. > It would be interesting to see samples. As you can see [1] the `while (true)` is lowered into `for (;true;)` so it's all about what construct pleases you the most. [1] https://github.com/dlang/dmd/blob/cd451ceae40d04f7371e46df1c955fd914f3085f/src/statementsem.d#L357 |
November 24, 2016 Re: OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Posted in reply to LiNbO3 | On Thursday, 24 November 2016 at 22:04:00 UTC, LiNbO3 wrote: > As you can see [1] the `while (true)` is lowered into `for (;true;)` so it's all about what construct pleases you the most. > > [1] https://github.com/dlang/dmd/blob/cd451ceae40d04f7371e46df1c955fd914f3085f/src/statementsem.d#L357 OK, thanks. The next question is: What principles guided when choosing between `for (;;) { ... }` and `while (true) { ... }` ? For example, there are two options: https://github.com/dlang/phobos/blob/master/std/algorithm/sorting.d |
November 24, 2016 Re: OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On 11/24/2016 05:09 PM, Dennis Ritchie wrote:
> On Thursday, 24 November 2016 at 22:04:00 UTC, LiNbO3 wrote:
>> As you can see [1] the `while (true)` is lowered into `for (;true;)`
>> so it's all about what construct pleases you the most.
>>
>> [1]
>> https://github.com/dlang/dmd/blob/cd451ceae40d04f7371e46df1c955fd914f3085f/src/statementsem.d#L357
>>
>
> OK, thanks.
>
> The next question is:
> What principles guided when choosing between `for (;;) { ... }` and
> `while (true) { ... }` ?
>
> For example, there are two options:
> https://github.com/dlang/phobos/blob/master/std/algorithm/sorting.d
I wouldn't ding anyone in a code review for using one vs the other. -- Andrei
|
November 25, 2016 Re: OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | Why you consider only 2 options? Use "do {} while (true);" :-) |
November 25, 2016 Re: OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Posted in reply to unDEFER | On 25/11/2016 8:27 PM, unDEFER wrote:
> Why you consider only 2 options?
> Use "do {} while (true);" :-)
The condition only executes after a single iteration.
So it is not the same code flow.
|
November 25, 2016 Re: OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On Friday, 25 November 2016 at 08:46:24 UTC, rikki cattermole wrote:
> On 25/11/2016 8:27 PM, unDEFER wrote:
>> Why you consider only 2 options?
>> Use "do {} while (true);" :-)
>
> The condition only executes after a single iteration.
> So it is not the same code flow.
For an the usecase infinite-loop the code has the same effect.
And will translate to the same machine-code, hopefully.
|
November 25, 2016 Re: OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Thursday, 24 November 2016 at 22:09:22 UTC, Dennis Ritchie wrote:
> On Thursday, 24 November 2016 at 22:04:00 UTC, LiNbO3 wrote:
>> As you can see [1] the `while (true)` is lowered into `for (;true;)` so it's all about what construct pleases you the most.
>>
>> [1] https://github.com/dlang/dmd/blob/cd451ceae40d04f7371e46df1c955fd914f3085f/src/statementsem.d#L357
>
> OK, thanks.
>
> The next question is:
> What principles guided when choosing between `for (;;) { ... }` and `while (true) { ... }` ?
>
> For example, there are two options:
> https://github.com/dlang/phobos/blob/master/std/algorithm/sorting.d
Between "for(;;)", "while(true)" and "do while(true)", I would use the "while (true) { }" for pure readability and semantic reasons.
I reckon "for(;;)" form is used for debug reasons (so you can easily insert conditions to transform an infinite loop into a finite one).
|
November 25, 2016 Re: OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Posted in reply to Claude | On 25.11.2016 11:33, Claude wrote: > ... > > Between "for(;;)", "while(true)" and "do while(true)", I would use the > "while (true) { }" for pure readability and semantic reasons. > ... What semantic reasons? > I reckon "for(;;)" form is used for debug reasons (so you can easily > insert conditions to transform an infinite loop into a finite one). You can just as easily edit the while condition. I use it because "unconditional loop" is less silly than "loop until true is false". |
November 25, 2016 Re: OT: for (;;) {} vs while (true) {} | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Friday, November 25, 2016 12:10:44 Timon Gehr via Digitalmars-d wrote:
> On 25.11.2016 11:33, Claude wrote:
> > ...
> >
> > Between "for(;;)", "while(true)" and "do while(true)", I would use the
> > "while (true) { }" for pure readability and semantic reasons.
> > ...
>
> What semantic reasons?
Probably the complete lack of a condition to test in for(;;). I confess that I was shocked when I found out that it was legal to have a for loop without a condition. That seems like doing while() or if(), which makes no sense. So, I never use for(;;) and wish that it didn't exist, but it does, and some folks use it. So, I have to live with the possiblity of dealing with it when dealing with code written by other people. But I won't ever use it.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation