Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A Why is a for loop getting stuck when using a uint64_t counter, whereas a while loop isn't?

SE user M Oehm provided with the following answer, as it can be found here. The condition i >= 0 is always true if i is an unsigned type. Decrementing an unsigned zero will not produce a nega...

posted 6d ago by aura-lsprog-86‭

Answer
#1: Initial revision by user avatar aura-lsprog-86‭ · 2025-02-15T07:49:27Z (6 days ago)
_SE user [M Oehm](https://stackoverflow.com/users/2979617) provided with the following answer, as it can be found [here](https://stackoverflow.com/a/35570694/5397930)._

<hr />

The condition `i >= 0` is always true if `i` is an unsigned type. Decrementing an unsigned zero will not produce a negative value, but the counter will wrap to the maximum representable number for the given type.

C represents ranges with an inclusive lower bound and an exclusive upper bound. For example, an array of `N` elements has indices `0` through `N - 1`. The upper bound itself isn't a valid array index.

This convention means that _you use a value before incrementing it, but decrement it before using it_. Consider a simple stack:

    stack[nstack++] = x;      // push a value
    x = stack[--nstack];      // pop a value

The same logic goes for loops: When you move forwards, use the value before you increment it:

    for (var i = 0; i < N; i++) { use(i); }

When you move backwards, decrement first and then use it:

    for (var i = N; i-- > 0; ) { use(i); }

This loop is equivalent to your `while`. The update section, which happens after processing the body, is empty here. The check is performed on the value before entering the loop; the loop body has the updated value.

This backwards loop might look awkward with the empty update section, but in other ways it is orthogonal to the forward version:

* It uses the actual bounds; there's no need to start with `N - 1`;
* It works equally well for arbitrary bounds as long as they follow the convention of inclusive lower and exclusive upper bound;
* The test is a pure inequality, not a greater/less-then-or equal comparison.