Running code only on the frame a condition becomes true

I have been wondering for quite a while how one would go about running a block of code once and only when a condition became true.

For example, adding to a variable when a boolean first returns true or things of the like.

You can use a property, like :

private bool _test;
public bool test
{
    get { return _test; }
    set
    {
        if (value != _test) // ignore if value isn't changing
        {
            _test = value; // assign the new value
            if (_test)
            {
                // do things if value turned true
            }
            else
            {
                // do things if value turned false
            }
        }
    }
}