One of the most common errors in development is where a loop or a retrieval by index falls short or long by one unit, usually because of an oversight or a logic in coding.
Of the following snippets, which one will run 10 times?
/* #1 */ for (N = 0 ; N < 10; N++) printf("%d\n", N);
/* #2 */ for (N = 0 ; N <= 10; N++) printf("%d\n", N);
/* #3 */ for (N = 1 ; N <= 10; N++) printf("%d\n", N);
/* #4 */ for (N = 1 ; N < 10; N++) printf("%d\n", N);
The question is deceptive, as there are two snippets that will run 10 times (1 and 3). But they will print different numbers. If you ware aiming for numbers from 1 to 10, only #3 is good.
After many years of programming, off-by-one errors are rare in my code, and I have been able to spot them or prevent them at first sight. That’s why I feel uneasy when I look at the way parallel replication is enabled in …
[Read more]