Question

Please help me interpret this for-loop:

for (byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
    ...do something
}

I think I understand (from Googling & StackOverflow) that 'color_bit' here is an UNSIGNED variable. So this means that the loop will count down from 23 to 0. But then what happens next? Does it attempt to start at 255 and continue to count down? But because the test is "!= 255" it will stop?

In my mind this seems to be the logic, but the tool I'm using (Arduino IDE) doesn't have a debugger, so I can't step through the code to see the values.

Was it helpful?

Solution

Your loop will see bit_color = 23..0 (24 iterations) provided 'byte' is 8 bit unsigned. Your loop stops when color_bit=255. HTH.

OTHER TIPS

You could add a serial print line as shown below:

 for (byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
      Serial.print(bit_color, DEC);
 }

To test/debug the counting sequence.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top