-1

I have an array I am reverse looping through. However, it seems if there is only one entry in my array, it doesn't loop at all.

for (uint8 i = fileNames.Num() - 1; i --> 0;)
{
  //Do stuff
}

Can anyone tell me why that is? Or what I can do to fix the loop conditions?

Dtb49
  • 1,211
  • 2
  • 19
  • 48
  • 5
    What is `fileNames.Num() - 1` in the case of a single element? What do you want your loop to do when `i` is `0`? – Tumbleweed53 Nov 30 '20 at 18:29
  • Its an array. i starts at the array's last index and approaches zero each loop – Dtb49 Nov 30 '20 at 18:30
  • 2
    Please step through the code with pen and paper for the case when there is a single element in `fileNames`. – Tumbleweed53 Nov 30 '20 at 18:31
  • 3
    Aside: Don't use `-->` It's misleading code. It's "clever", but reviewers will need to stop a minute and think "was that intentional?" – AndyG Nov 30 '20 at 18:32
  • In general, don't update your condition variable in the condition. It leads to logic issues like you currently have. – ChrisMM Nov 30 '20 at 18:33
  • 3
    arrays do not have a `Nums` member. Please include a [mcve] of your code in the question – 463035818_is_not_an_ai Nov 30 '20 at 18:33
  • 2
    This works `for (uint8 i = fileNames.Num() ; i-- > 0;)` assuming `Num()` is the size of your array. This is pretty idiomatic for a backwards for loop I think. – john Nov 30 '20 at 18:34
  • Is there some tutorial somewhere that is teaching people to do this `i --> 0` thing? I remember seeing that expression as a joke or like a puzzle years ago but in the last year I've seen at least one other post on SO where someone seems to think it is a real idiom. – jwezorek Nov 30 '20 at 18:37
  • 2
    Unless you have a really specific reason to do otherwise, I'd advise using a `std::vector` or `std::array`, and stepping backwards through it using a `reverse_iterator`. – Jerry Coffin Nov 30 '20 at 18:38
  • @jwezorek perhaps it was this what you saw: https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c The "goes to operator" – 463035818_is_not_an_ai Nov 30 '20 at 18:40
  • Beautiful loop pattern ! – Florent Nov 30 '20 at 19:23

1 Answers1

0

The code you posted means the same thing as the following:

uint8 i = filenames.Num() - 1
while ( (i--) > 0) {
   //do stuff
}

If filename.Num() is 1 then the loop body does not execute because 1 - 1 is not greater than zero.

This expression i --> 0 is not idiomatic C++ and has no place in real code. It is a programming language pun, and to some extent a joke someone posted on Usenet at the expense of newbies akin to sky hooks or snipe hunts in other social domains e.g. "Hey did you know about the secret operator in C++???"

jwezorek
  • 8,592
  • 1
  • 29
  • 46
  • I understand the logic behind it. lol The real issue actually wasn't with the operator (well sort of) It was because I was using `uint8` instead of `int` therefore the loop would actually never terminate because it would just loop from 0-255. lol – Dtb49 Nov 30 '20 at 21:43