www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "For" infinite loop

reply "RivenTheMage" <riven-mage id.ru> writes:
This is infinite loop:

for (ubyte i=0; i<=255; i++)
{
       ...
}

I guess it's a bug?
Aug 11 2012
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
A ubyte is ALWAYS <=255, since ubyte 255 + 1 == 0.
Aug 11 2012
parent reply "RivenTheMage" <riven-mage id.ru> writes:
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
parent reply "RivenTheMage" <riven-mage id.ru> writes:
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:
 A ubyte is ALWAYS <=255, since ubyte 255 + 1 == 0.
Isn't both "i" and "255" should be propagated to int before comparison?
Implicitly propagated, I mean.
Aug 11 2012
parent reply "Chris Cain" <clcain uncg.edu> writes:
On Saturday, 11 August 2012 at 19:20:36 UTC, RivenTheMage wrote:
 Isn't both "i" and "255" should be propagated to
 int before comparison?
Implicitly propagated, I mean.
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.
Aug 11 2012
parent "RivenTheMage" <riven-mage id.ru> writes:
Okay, thanks for helping!
Aug 11 2012
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le samedi 11 ao=C3=BBt 2012 =C3=A0 20:48 +0200, bearophile a =C3=A9crit :
 RivenTheMage:
=20
 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
n this case why not using a while loop ?
Aug 11 2012
next sibling parent "Chris Cain" <clcain uncg.edu> writes:
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
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply "MattCoder" <mattcoder hotmail.com> writes:
On Saturday, 11 August 2012 at 20:38:33 UTC, bearophile wrote:
 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
What about: foreach(ushort i; 0..256) writefln("%d", cast(ubyte)i); It may look better than for loop. ("Maybe").
Aug 12 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply Marco Leise <Marco.Leise gmx.de> writes:
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,
 bearophile
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. -- Marco
Aug 13 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling parent "MattCoder" <mattcoder hotmail.com> writes:
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>:

 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
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.
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.
Aug 13 2012