2 ms·
There's a more concise way of iterating in reverse. for (size_t i = numBurritos; i-- > 0;) if (wantBurrito(i)) break; Where an advantage is that m
by scj 11y ago
There's a more concise way of iterating in reverse.
for (size_t i = numBurritos; i-- > 0;)
if (wantBurrito(i))
break;
Where an advantage is that many assembly languages have a "jump if zero" or "jump if not zero" instruction.
EDIT: It appears the article has been updated to reflect this method.
- efaref 11y agoI've seen this referred to as the "goes-to" operator: size_t i = num_burritos; while (i --> 0) // Read: while 'i' goes to zero if (want_burrito(i)) break; While it sounds nice, I don't think it's a good idea. Clearly the correct solution is to use a better language, e.g.: return next((burrito for burrito in reversed(burritos) if burrito.wanted()), None)