4

I have a for loop where I'm using the slide operator as I'm working with unsigned types. Essentially it boils down to

for (std::size_t i = 6; i --> 0;){
    cout << i;
}

But it outputs the numbers from 5 to 0 inclusive and omits 6. Why?

Thank you very much for looking at this. I'm quite stuck.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    I'm at a loss here. I *should* upvote this since the question is well-written with a nice example. But the concepts behind it are awful. Why would someone write code like this? – Bathsheba Aug 02 '16 at 15:52
  • 2
    "_Slide_" operator?? You are just doing this `(i--) > 0;` – Khalil Khalaf Aug 02 '16 at 15:52
  • 1
    @Bathsheba, the question opens the door for discussion about the awfulness of its topic. Since it's also well written, I'd say it's a very good question. – StoryTeller - Unslander Monica Aug 02 '16 at 15:55
  • Actually, this is precisely what the "slide operator" should do: iterate over the indices in the interval `[0, count[`, where zero is assumed to be included while `count` is excluded. Just like how you index an array in C. Thus, with a vector `array`, the following is correct: `for(size_t i = array.size(); i --> 0; ) {...}`, or even shorter: `for(size_t i = array.size(); i--; ) {...}`. – cmaster - reinstate monica Aug 02 '16 at 16:31

3 Answers3

7

This is a touchstone for

  1. The fact that this so-called "operator" should be used with caution, if at all.

  2. Changing the state of variables within the conditional check of a for loop ought to be done with extreme caution, if at all.

The largest output is 5 simply because i is decremented as a result of the conditional test which also decrements i. The conditional check is ran before program control enters the for loop body.

You really ought to rewrite the loop. Don't set the initial value of i to 7 as that's a dirty hack. Although --> is often used with while (and to emphasise, it's unlikely to win you many friends in your programming review), I've never seen it used with a for loop before.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
4

--> is not a slide operator.

It is understood by the compiler as two different operators -- and >.

So your code look like this to the compiler:

for (std::size_t i = 6; (i--) > 0;){
    cout << i;
}

Since the loop condition is checked before entering the loop's body i is decreased before the first execution of the loop's body, hence the produced sequence of numbers is 5 4 3 2 1 0.

For more details see this Stack Overflow question: What is the "-->" operator in C++?

Community
  • 1
  • 1
Nemanja Trifunovic
  • 3,017
  • 1
  • 17
  • 20
1

After evaluating this condition in the for statement

i --> 0

i will be equal to 5. So the first iteration of the loop outputs

5

To achieve the effect you want rewrite the loop the following way

#include <iostream>

int main() 
{
    size_t i = 6;

    do
    {
        std::cout << i;
    } while ( i-- > 0 );

    return 0;
}

The program output is

6543210

Another way is the following

#include <iostream>

int main() 
{
    for ( size_t i = 6; std::cout << i && i != 0; i-- )
    {
        //
    } 

    return 0;
}

Of course you could write the loop like this

const size_t N = 6;

for ( size_t i = N + 1; i-- > 0; )
//              ^^^^^^^    
{
    std::cout << i;
}

However in general this approach does not work when the initial value of i is equal to std::numeric_limits<size_t>::max() Because if to add 1 to this value you will get 0.

A general approach using a for loop can look the following way

#include <iostream>

int main() 
{
    const size_t N = 6;

    for ( size_t i = N, j = N; j != 0; j = i-- )
    {
        std::cout << i;
    } 

    return 0;
}

Its output is

6543210
Matt
  • 74,352
  • 26
  • 153
  • 180
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335