Tracking Boolean State Changes

How do I handle situations where I need to track the history of boolean state changes?

When you need to remember previous boolean states, you can store the old value in a separate variable before updating the current state. This is especially useful for detecting changes or transitions in your program's state.

Basic State Tracking

Here's a simple example of tracking a state change:

#include <iostream>
using namespace std;

int main() {
  bool wasAlive{true}; // Previous state
  bool isAlive{true}; // Current state
  int Health{100};

  // Store the previous state before updating
  wasAlive = isAlive;

  // Update current state based on health
  Health = Health - 150; // Take massive damage
  isAlive = Health > 0;

  // We can now check if the state just changed
  bool justDied{wasAlive && !isAlive};

  if (justDied) {
    cout << "Game Over - you just died!\n";
  } else if (!wasAlive && !isAlive) {
    cout << "Still dead\n";
  } else if (wasAlive && isAlive) {
    cout << "Still alive\n";
  }
}
Game Over - you just died!

Tracking Multiple State Changes

You can track multiple state changes by keeping multiple previous states:

#include <iostream>
using namespace std;

int main() {
  // Track battle status
  bool wasInCombat{false};
  bool isInCombat{false};
  bool hasWeapon{true};

  // First update
  wasInCombat = isInCombat;
  isInCombat = true;

  bool justEnteredCombat{
    !wasInCombat && isInCombat};
  if (justEnteredCombat) {
    cout << "Combat started!\n";
    if (!hasWeapon) {
      cout << "Warning: Entered combat without "
        "a weapon!\n";
    }
  }

  // Second update
  wasInCombat = isInCombat;
  isInCombat = false;

  bool justLeftCombat{
    wasInCombat && !isInCombat};
  if (justLeftCombat) {
    cout << "Combat ended!";
  }
}
Combat started!
Combat ended!

By storing the previous state, we can detect important transitions in our program, like when a player enters or leaves combat, when they gain or lose an ability, or when they cross important thresholds.

This is particularly useful in games and interactive applications where you need to trigger specific actions when states change.

Booleans - true and false values

An overview of the fundamental true or false data type, how we can create them, and how we can combine them.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Why Booleans Take a Byte of Memory
If booleans only store true/false, why do they take up a whole byte of memory?
Safe Floating-Point Comparisons
How do I handle floating-point comparisons when the numbers might not be exactly equal?
Optimizing Boolean Logic
How can I optimize boolean expressions when I have many conditions to check?
Not Operator vs Equals False
When should I use ! versus == false when checking if a boolean is false?
Debugging Complex Boolean Logic
How do professional programmers debug complex boolean expressions?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant