Raphael
Raphael

Reputation: 127

Increment expression for 'for' doesn't have any effect

I have a class, Square:

class Square
{
   typedef enum {kTop = 0x80, kRight = 0x40, kBottom = 0x20, kLeft = 0x10} Side;
   // Other functions and stuff
}

In one of my functions, I have a for loop that performs an operation involving each of the sides of a Square. I made it like this:

for (Square::Side side = Square::kLeft; side < Square::kLeft; side << 1) // Warning here
{
   // Do stuff
}

First, the loop would work on the left side, and then it would shift the side one position to the left, making the side equal to the bottom side. After the for loop has performed its operations on the Top side, it would shift left again, pushing the bit off of the number and making it equal to 0, which is less than kLeft, and the loop would end.

However, it's giving me a warning that says "for increment expression has no effect". Does this mean that my shift operation is not happening?

Upvotes: 2

Views: 3751

Answers (2)

Richard Sitze
Richard Sitze

Reputation: 8463

You're not assigning the value of the shift back to side. Try this:

(Square::Side side = Square::kLeft; side < Square::kLeft; side = side << 1)

or side <<= 1.

You'll also need to fix your terminating condition:

side <= Square.kTop

Upvotes: 3

Luchian Grigore
Luchian Grigore

Reputation: 258618

Yes it does, because the stop condition is false on the first attempt:

Square::Side side = Square::kLeft

renders

side < Square::kLeft;

false.

Perhaps you meant side <= Square::kTop or similar and side <<= 1.

Upvotes: 3

Related Questions