Let's say I want to iterate over all integers in a for
loop. For the sake of discussion, assume I am calling some unknown function f(unsigned x)
for each integer:
for (unsigned i = 0; i < UINT_MAX; i++) {
f(i);
}
Of course, the above fails to iterate over all integers, because it misses one: UINT_MAX. Changing the condition to i <= UINT_MAX
just results in an infinite loop, because that's a tautology.
You can do it with a do-while
loop, but you lose all the niceties of the for
syntax.
Can I have my cake (for
loops) and eat it too (iterate over all integers)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…