Tag Archives: c++

An infinite loop bug

It’s the coolest infinite loop I had ever fallen into:

for(char c = 0; c <= 127; c++) {...}

I wonder how I’m supposed to perform the intended iterations without obfuscating the code. Sure, I can just drop char and use int instead, but this does not solve the problem in general.

P.S.: There seem to be no syntax primitive to safely iterate over an interval [L; U] in languages from the C/Java family. This means that you just cannot write code for(T c = L; c <= U; c++) {...} without making assumptions on T and U.

P.P.S.: Pascal rules! Code for c:= 0 to 255 do begin ... end, where c is a byte works just fine 🙂