[–] kenferry 2 hours ago
while (i --> 0) {
// use array[i]
}
easy to read (if not understand), and correct![–] krackers 2 hours ago
https://stackoverflow.com/questions/1642028/what-is-the-oper...
[–] cryptonector 48 minutes ago
[–] viega 1 hour ago
while (i-- != 0) { ... }
It's more clear than the most idiomatic way, which is to drop the explicit comparison: while (i--) { ... }
All of it is better than the clunky old for-loop syntax.[–] chii 40 minutes ago
Usually, this won't happen of course, but because `i` is not declared inside a for-loop, it must have been declared outside, and thus might even look like a regular variable that could be used by some "clever" coding to multipurpose it!
In any case, it's a niche worry i have, so i usually don't do it if i know there's a for loop that would do.
[–] mtklein 1 hour ago
[–] stephencanon 2 hours ago
(Hi Ken!)
[–] kooi 24 minutes ago
for (int i = 1; i <= N; i++) {
array[N-i]
}[–] bugake 56 minutes ago
It also says not to use `for (i >= 0)` because any unsigned integer is >= 0, and the loop will never end.
[–] randomblock1 30 minutes ago
for (size_t i = count; i > 0; --i) {
// use array[i - 1]
}
You can also do something like: size_t index = i - 1;
to not have to use i-1. I believe the compiler optimizes that out anyway to be identical.[–] seeknotfind 13 minutes ago
[–] AdieuToLogic 1 hour ago
_Bool find_last (size_t count, int *haystack, int needle, size_t *where)
{
for (const int *pos = haystack + count; pos-- != haystack;) {
if (*pos == needle) {
*where = pos - haystack;
return 1;
}
}
return 0;
}[–] i2talics 42 minutes ago
[–] MaulingMonkey 2 hours ago
const size_t n = 10;
for (size_t i = n; i --> 0;) {
std::cout << i << "\n";
}[–] hedgehog 2 hours ago
[–] omoikane 1 hour ago
https://github.com/thaliaarchi/bfcorpus/blob/main/ebosman/ma...
Be sure to check out other files in that directory for how the program is assembled.
(Erik Bosman's mandelbrot set is a personal favorite of mine, very useful for benchmarking brainfuck interpreters)
[–] ranger_danger 2 hours ago
VHDL's array/vector syntax actually works a similar way:
-- gives 0, 1, 2, 3:
for i in 0 to 3 loop
-- gives 3, 2, 1, 0:
for i in 3 downto 0 loop
-- Source - https://stackoverflow.com/a/7642026 -- Posted by Kerrek SB -- Retrieved 2026-10-29, License - CC BY-SA 3.0
[–] fourthark 1 hour ago
[–] ranger_danger 1 hour ago
For me it does not work because I'd want to use it with values < 0, but the for loop checks for > 0, so I would have to modify that first.
[–] Joker_vD 22 minutes ago