Operation to "Get and AND" boolean variable in Java

Is it possible in Java to use syntax like (i++, ++i) for boolean logic operators?

I have a boolean variable that is true only for the first iteration of a foreach loop. That iteration has to be skipeed.

Full syntax is

for (...)
{
    if (bool)
    {
        bool &= false;
        continue;
    }
}

I wonder if there is any way to shorten the syntax without using AtomicBoolean. For example construct if (bool &= false) is syntactically correct but I think it will compare the final result and not the original value.

Google is not my friend because the search query is misleading

Jon Skeet
people
quotationmark

Personally I would simplify your current code to:

for (...)
{
    if (bool)
    {
        bool = false;
        continue;
    }
    // Rest of code
}

... but if you really want to do it in the if condition as a side-effect, you could use:

for (...)
{
    if (bool && !(bool = false))
    {
        continue;
    }
    // Rest of code
}

Here the first operand of the && operator covers subsequent operations, and !(bool = false) will always evaluate to true and set bool to false.

Another option, from comments:

for (...)
{
    if (bool | (bool = false))
    {
        continue;
    }
    // Rest of code
}

This performs the assignment on each iteration, but it still gives the right result each time.

I really, really wouldn't use either of these last two options though.

people

See more on this question at Stackoverflow