3

I am a little confused why this is not working:

id = (isChar ? (id + 1 > 122 ? 65 : id++) : id++);

The input here can either be an int or a char converted to an INT. I am then incrementing the id and increasing either the int or char. The problem is, when I input a char, the number does not seem to change?

2
  • input a char? please show all the relevant code. Commented Mar 7, 2012 at 16:11
  • How about make your code readable first. I see a lot of magic numbers. Commented Mar 7, 2012 at 16:12

3 Answers 3

14

This is an extremely poor programming practice that you're using. Conditional expressions should not have side effects; they should compute values. You are executing the side effect and then throwing away the side effect! You should either (1) make a side-effect-free version:

id = (isChar && id > 121) ? 65 : id + 1;

or (2) write your side-effecting version as statements, not expressions:

if (isChar && id > 121)
  id = 65;
else 
  id++;

Let's take a look in more detail what is wrong with this simplified version of your original buggy code:

id = whatever ? 65 : id++;

Suppose whatever is false. What happens? id++ is morally equivalent to:

int PostIncrement(ref int x)
{
    int temp = x;
    x = temp + 1;
    return temp;
}

So suppose you did:

id = whatever ? 65 : PostIncrement(ref id);

What happens? Suppose id is 1. You pass it by reference to PostIncrement. PostIncrement makes a copy of the value of id -- 1 -- in temp. It then adds one to that -- 2 -- and assigns the result to id. So id is now 2. Then it returns 1.

Back in the caller, id is now 2, and then you assign the result of PostIncrement, which was 1, and now id is 1 again.

Do not use id++ to mean id + 1 because that is not at all what it means.

Sign up to request clarification or add additional context in comments.

Comments

7

Change id++ to id + 1 in both cases. You are throwing away the change from the increment in the assignment, which is executed last.

As a general rule, avoid side-effects (such as ++) in complex expressions. They make the whole expression intractable. This has tripped you up here.

Better yet, increment id beforehand since you always seem to increment it:

id += 1;
if (isChar && id > 122)
    id = 65;

or

id = (isChar && id > 121) ? 65 : id + 1;

1 Comment

Also adding some parenthesis may make it a little easier to read.
0

The other answers are correct. You should consider their advice first, however, it is possible to fix this simply by moving the ++ operator before the variable, i.e. ++id.

Essentially, putting ++ after your variable (postfix increment operation) returns the value of the variable before the increment occurs. By moving the ++ ahead of your variable (prefix increment operation), it returns the value of the variable after the increment is performed. Note that in both cases the incremented value is still stored in the variable, only the value returned by the operation is affected.

See ++ Operator (C# Reference) for more details on that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.