digitalmars.D.learn - "For" infinite loop
- RivenTheMage (6/6) Aug 11 2012 This is infinite loop:
- Adam D. Ruppe (1/1) Aug 11 2012 A ubyte is ALWAYS <=255, since ubyte 255 + 1 == 0.
- RivenTheMage (3/4) Aug 11 2012 Isn't both "i" and "255" should be propagated to
- RivenTheMage (2/7) Aug 11 2012 Implicitly propagated, I mean.
- Chris Cain (18/21) Aug 11 2012 Regardless, a ubyte 0 converted to an int is still 0.
- RivenTheMage (1/1) Aug 11 2012 Okay, thanks for helping!
- bearophile (12/18) Aug 11 2012 One way to scan all the ubytes with a for loop:
- bioinfornatics (2/26) Aug 11 2012 n this case why not using a while loop ?
- Chris Cain (23/24) Aug 11 2012 You mean like this?
- bearophile (8/9) Aug 11 2012 It uses less lines of code, and with the for loop you have a
- MattCoder (5/14) Aug 12 2012 What about:
- bearophile (6/9) Aug 12 2012 It looks better. But you every time you use a cast in D you need
- Marco Leise (5/11) Aug 13 2012 So true. It happens to me sometimes that I cast away const although I ju...
- bearophile (7/11) Aug 13 2012 Marco Leise:
- MattCoder (14/26) Aug 13 2012 In fact, it would be nice to do something like this:
This is infinite loop: for (ubyte i=0; i<=255; i++) { ... } I guess it's a bug?
Aug 11 2012
A ubyte is ALWAYS <=255, since ubyte 255 + 1 == 0.
Aug 11 2012
On Saturday, 11 August 2012 at 18:37:18 UTC, Adam D. Ruppe wrote:A ubyte is ALWAYS <=255, since ubyte 255 + 1 == 0.Isn't both "i" and "255" should be propagated to int before comparison?
Aug 11 2012
On Saturday, 11 August 2012 at 19:18:14 UTC, RivenTheMage wrote:On Saturday, 11 August 2012 at 18:37:18 UTC, Adam D. Ruppe wrote:Implicitly propagated, I mean.A ubyte is ALWAYS <=255, since ubyte 255 + 1 == 0.Isn't both "i" and "255" should be propagated to int before comparison?
Aug 11 2012
On Saturday, 11 August 2012 at 19:20:36 UTC, RivenTheMage wrote:Regardless, a ubyte 0 converted to an int is still 0. a ubyte can only hold a maximum of 255 and will roll over to 0 when incremented at that point. So, i = 0 i <= 255 (0 <= 255 is true) // do stuff ++i (i = 1) i <= 255 (1 <= 255 is true) // do stuff ... ++i (i = 255) i <= 255 (255 <= 255 is true) // do stuff ++i (i = 0) i <= 255 (0 <= 255 is true) ... go on infinitely.Isn't both "i" and "255" should be propagated to int before comparison?Implicitly propagated, I mean.
Aug 11 2012
RivenTheMage:This is infinite loop: for (ubyte i=0; i<=255; i++) { ... } I guess it's a bug?One way to scan all the ubytes with a for loop: import std.stdio; void main() { for (ubyte i = 0; ; i++) { write(i, " "); if (i == 255) break; } } Bye, bearophile
Aug 11 2012
Le samedi 11 ao=C3=BBt 2012 =C3=A0 20:48 +0200, bearophile a =C3=A9crit :RivenTheMage: =20n this case why not using a while loop ?This is infinite loop: for (ubyte i=3D0; i<=3D255; i++) { ... } I guess it's a bug?=20 One way to scan all the ubytes with a for loop: =20 import std.stdio; void main() { for (ubyte i =3D 0; ; i++) { write(i, " "); if (i =3D=3D 255) break; } } =20 Bye, bearophile
Aug 11 2012
On Saturday, 11 August 2012 at 20:00:40 UTC, bioinfornatics wrote:n this case why not using a while loop ?You mean like this? void main() { ubyte i = 0; do { write(i, " "); } while(i++ != 255); } or void main() { ubyte i = 0; while(true) { write(i, " "); if(i++ == 255) break; } } ? It's kind of a personal preference on how you want to handle this case, IMO. I think using a for loop how bearophile showed is more typical. Most would argue going with the "standard" approach is the most correct way ... but I almost like using a do-while in this case.
Aug 11 2012
bioinfornatics:n this case why not using a while loop ?It uses less lines of code, and with the for loop you have a single place where to put the loop variable initialization, test and increment. This makes the code simpler to read. In this case the test is moved inside the loop, but it's better still than a regular while loop. Bye, bearophile
Aug 11 2012
On Saturday, 11 August 2012 at 20:38:33 UTC, bearophile wrote:bioinfornatics:What about: foreach(ushort i; 0..256) writefln("%d", cast(ubyte)i); It may look better than for loop. ("Maybe").n this case why not using a while loop ?It uses less lines of code, and with the for loop you have a single place where to put the loop variable initialization, test and increment. This makes the code simpler to read. In this case the test is moved inside the loop, but it's better still than a regular while loop. Bye, bearophile
Aug 12 2012
MattCoder:foreach(ushort i; 0..256) writefln("%d", cast(ubyte)i); It may look better than for loop. ("Maybe").It looks better. But you every time you use a cast in D you need to be careful, because they are like a sharp tool with almost no safeties. Bye, bearophile
Aug 12 2012
Am Mon, 13 Aug 2012 00:53:43 +0200 schrieb "bearophile" <bearophileHUGS lycos.com>:It looks better. But you every time you use a cast in D you need to be careful, because they are like a sharp tool with almost no safeties. Bye, bearophileSo true. It happens to me sometimes that I cast away const although I just want to cast a type. It's also a reason I am not a big fan of considering "cast(ubyte) 0" normal. It is pragmatic though. -- Marco
Aug 13 2012
Marco Leise: Again it seems my message you have answered to is not visible through the online reader at forum.dlang.org.So true. It happens to me sometimes that I cast away const although I just want to cast a type. It's also a reason I am not a big fan of considering "cast(ubyte) 0" normal. It is pragmatic though.A not a lot documented way to remove a const it to use an empty cast() with no argument. Another way is to use unqual!(). Bye, bearophile
Aug 13 2012
On Monday, 13 August 2012 at 08:18:08 UTC, Marco Leise wrote:Am Mon, 13 Aug 2012 00:53:43 +0200 schrieb "bearophile" <bearophileHUGS lycos.com>:In fact, it would be nice to do something like this: foreach(ubyte i; 0..256) writefln("%d", i); but the code above will generate an error like: 256 cannot be converted to ubyte! On the other hand: foreach(ubyte i; 0..255) writefln("%d", i); Works, but only will show numbers from 0..254, without "255"! So, maybe this is more safer: foreach(ushort i; 0..ubyte.max+1) writefln("%d", cast(ubyte)i); Matheus.It looks better. But you every time you use a cast in D you need to be careful, because they are like a sharp tool with almost no safeties. Bye, bearophileSo true. It happens to me sometimes that I cast away const although I just want to cast a type. It's also a reason I am not a big fan of considering "cast(ubyte) 0" normal. It is pragmatic though.
Aug 13 2012