How to tell if the code flow changed

Alright, so this is something I’ve never quite figured out how to do efficiently.

You could have:

bool last;
//true = was last in the if statement
//false = was last in the else statement

if (/*not important*/)
{
       if (!last)
       //Special code
       last = true;

   //Normal Logic
}
else
{
       if (last)
       //Special code
       last = false;

   //Normal Logic
}

This is a technique that I’ve always used to circumvent this problem, but I was just wondering if there was a more efficient way of doing this.

The situation I’m dealing with now is I want to switch between two different camera positions and be able to tell WHEN it switches modes. If I know when it switched, I’ll use some special code to lerp it to the correct position.

I’m sure I could use this method or something similar, but I just wanted to minimize my use of variables.

Thanks if anyone can help!

Once again, I show just how noobish I really am to coding. Basically the solution I was looking for is found in FSM.

Anyone who wants to learn more can follow these links:
http://gameprogrammingpatterns.com/state.html

If all you need is to know when a bool value changed, you can simply use a property (variable + accessor), like :

private bool _state;
public bool State
{
	get { return _state; }
	set
	{
		if (value != _state) // is value changed
		{
			// do whatever based on old value
			_state = value; // assign new value to variable
			// do whatever based on new value
		}
	}
}