Search
"For" infinite loop
Aug 11, 2012
RivenTheMage
Aug 11, 2012
Aug 11, 2012
RivenTheMage
Aug 11, 2012
RivenTheMage
Aug 11, 2012
Chris Cain
Aug 11, 2012
RivenTheMage
Aug 11, 2012
bearophile
Aug 11, 2012
bioinfornatics
Aug 11, 2012
Chris Cain
Aug 11, 2012
bearophile
Aug 12, 2012
MattCoder
Aug 12, 2012
bearophile
Aug 13, 2012
Marco Leise
Aug 13, 2012
bearophile
Aug 13, 2012
MattCoder
```This is infinite loop:

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

I guess it's a bug?

```
```A ubyte is ALWAYS <=255, since ubyte 255 + 1 == 0.
```
```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
```
```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?
```
```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.
```
```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.
```
```Okay, thanks for helping!
```
```Le samedi 11 août 2012 à 20:48 +0200, bearophile a écrit :
> 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

n this case why not using a while loop ?

```
```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.
```
```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
```
« First   ‹ Prev
1 2