I was wondering if there is a clean way of counting from 0 to 255 using an 8 bit datatype, something like:
for(uint8_t i(0);i<=255;++i)
{
....
}
This obviously will not work but it makes it clear you want to count from 0 to 255.
A working solution would be something like:
uint8_t i(0);
do
{
...
++i;
}
while(i > 0);
But here it is not at all clear it counts from 0 to 255.
This will also work but it is just ugly IMHO:
uint8_t i(0);
while(true)
{
...
if (i == 255)
{
break;
}
++i;
}
So I was wondering, is there a clean way of doing this without using a larger datatype?
EDIT:
- I like the version using for because it makes its intend clear without thinking: looping from 0 to 255. All other versions require some thought about what is going on and therefore more likely to confuse others.
- I do not want to use int because the code is for a 8-bit microcontroller with not much memory.