tags:

views:

140

answers:

3
for (int i = 0; i < X; i++)
   myitem = (checkedDB) ? dirtyItem : cleanItem;

I wanted to know if there's a way of flipping checkedDB in the same statement, i.e. the next iteration checkedDB is the opposite of it's value, so like XORing.

+4  A: 

What about:

for (int i = 0; i < X; i++)
    myitem = !(checkedDB = !checkedDB) ? dirtyItem : cleanItem;

That may be not really readable/understandable at first sight, but it does what you want in one statement.

Philip Daubmeier
won't this flip the value first and then check the flipped value? i'm only guessing. sorry if i'm wrong. :)
echo
`checkedDB = !checkedDB` will evaluate to `!checkedDB`, thus you should swap `dirtyItem` and `cleanItem` if it should be semantically equivalent to the formulation in the question.
aioobe
Yeah youre right, now this works, just tested :)
Philip Daubmeier
Nope. It flips the value, sets the variable to that value, then checks against the value flipped again (i.e. the original value).
Chuck
I think it's important to note that this is **not something you should ever voluntarily do**.
Chuck
@Chuck: yes, yes, I corrected it already :)
Philip Daubmeier
@Chuck: agreed. Thats everything else than clean code. It may look like `checkedDB == !checkedDB` at first sight. But it is just the answer to the OPs question.
Philip Daubmeier
@Philip: Yeah, I know. Like I said, I just think it's important to note that, although this is the best way to do what the OP is asking about, it's even better not to do it in the first place. Nothing against your answer.
Chuck
A: 

One way to toggle a boolean value is bool ^= true:

for (int i = 0; i < X; i++)
{
   myitem = (checkedDB ^= true) ? cleanItem : dirtyItem;
}

I swapped cleanItem and dirtyItem since you toggle checkedDB before one of them is chosen.

The benefit of using checkedDb ^= true over checkedDB = !checkedDB is that it is clear that you meant to modify checkedDB and didn't accidentally mistype a == comparison.

Since you haven't specified the language, I can't say for certain if your language will allow an assignment in the conditional part of the ternary operator.

Mark Rushakoff
C, C++, C# and Java all allow this, dont they? I am just guessing the OP meant one of these four.
Philip Daubmeier
What is the benefit of this over just doing `x = !x`?
Chuck
I don't have a C# or Java compiler handy to test it, but C and C++ will certainly compile it. Without testing it, I'm guessing C# would allow it since you aren't using a single `=`... Is there somewhere I can run a C# snippet online? :/
Mark Rushakoff
@Chuck: The benefit is that it is clear that your intent was a *modifying assignment*, not a typo where you intended to use an `==`.
Mark Rushakoff
Just tried it with c#: my code of the form `a = !(b = !b) ? t : f;` compiles and does the right thing, your `a = (b ^= true) ? t : f;` does also compile and does the trick, too.
Philip Daubmeier
Youre right, your method makes more clear that it wasnt a typo, however I think, swapping bools reads simpler with just the `!` operator. I guess both solutions are nice, small and inline, but both are not very nice with respect to maintainability/readability.
Philip Daubmeier
@Mark: we both overread the headline that says 'C# - ...' :)
Philip Daubmeier
+3  A: 

The best answer, IMO, is: not if you have any self-respect. The result will be ugly and confusing, and for no real gain. Here are two distinct solutions that are cleaner and thus easier to understand.

for (int i = 0; i < X; i++)
{
    myitem = checkedDB ? dirtyItem : cleanItem;
    checkedDB = !checkedDB;
}

The following version doesn't even require the extra variable and achieves your one-line goal:

for (int i = 0; i < X; i++)
{
    myitem = i%2 == 0 ? dirtyItem : cleanItem;
}
Marcelo Cantos
Agreed. I should have given the OP that advice, too. :)
Philip Daubmeier