What's the programming language for you?

for (int i=0; i!=0; i++) printf("%i", i); - this will exit without ever running the loop, because it evaluates the condition before entering the first time

for (int i=1; i!=0; i++) printf("%i", i); - this will print all ints except 0, including the negatives, because when you overflow, it wraps around from MAX_INT (0x7FFFFFFF) to MIN_INT (0x800000000). This is technically undefined behaviour, but every compiler will handle it nicely the way you’d expect.

(hex values provided assume 32 bit int, which is standard in the contexts I work in)

ETA: apparently not all compilers! GCC can handle this case differently depending on the settings you compile with (-fwrapv)

4 Likes