Question about Booleans (Specifically with the OnInput Function

void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else // checking the player guess
    {
        ProcessGuess(PlayerInput);
    }
}

Why is it that the bool here, bGameOver, does not say if bGameOver = true ?
It seems like if the game is over we want to clear the screen and setup the game.
else (if the game is not over, hence bGameOver = false) we go ahead and process the player input.
I don’t really understand why this works… it seems like by not assigning a true or false value we are basically saying check to see IF bGameOver is true OR false. which to me the code is basically saying:

    if (bGameOver == true || false)
    {
        ClearScreen();
        SetupGame();
    }

Which isn’t what we want, right? (disregarding the fact that this may be improperly written code)

I see that we assign the bGameOver bool as false in the SetupGame function, and true in the EndGame function. That makes sense to me in a general way. While we are setting up the game we don’t want the game to be over. Yet when the end of the game is reached of course we want the game to be over. But when the player makes his input we are allowing the bGameOver bool (which can only be true or false) to be undecided?

The instructor says in the video. “You don’t have to add true to the end of bGameOver because this is going to be either true or false right from the offset.” But then he just moves on and doesn’t say anything else about it. That leaves so much unsaid in my mind… Please, what am I missing?

This goes back to C actually. Back when the language was created, you had int, char and if you were lucky, float as well as a few other basic types that were a variation on a theme, but no bool.
The language created logic for the if statement where 0 (zero) is false and anything else that is not 0 is considered true.

So, C being quite a powerful language, they used Pre-Processor commands to define BOOL as int.

#define BOOL int
#define FALSE 0
#define TRUE !FALSE

so, for example true is basically !0

Spin forward to modern times, the C++ standards introduce the bool type. Meanwhile if statements still look for either true or false and guess what, bool can be assigned true or false.

This means you do not and in fact really should not compare a bool to true or false for efficiency purposes.

This is also the same reason why you often see pointers being checked without comparing to null in a similar way - because NULL = 0, defined in the same manner as FALSE (nullptr is also similar but more/better and also came a good bit later as part of the C++ 11 standards) and so if (ptr) { } means if the pointer is not null.

basically,

if (bool condition) {
  //do the positive outcome i.e. condition is true
}
else
{
  //do the negative outcome i.e. condition is false
}

Where any condition, be it a variable of type bool, an integer assigned a number, a pointer, the return value that is a bool from a function call (or integer) or a comparison/range results in an outcome of true or false. For bool variables, they are already either true or false.

Given this information, I find it good practice to always specify ptr!=nullptr and not just ptr, and similar intValue != 0 rather than just intValue.

It has 2 benefits. 1, it is clear what you are doing and 2, it is also clear what kind of variable you’re dealing with. It’s similar with comparing floats aValue == 0.0f is clear, although you should never do an equals with a float - it’s unpredictable.

I hope this helps

The condition isn’t expecting a comparison, it’s expecting a boolean value which is exactly what bGameOver stores.

An if statement is executed if the condition is true, again this doesn’t necessarily require a comparison. If bGameOver stores the value true then the code within it will execute.

And just to add

bGameOver == true || bGameOver == false

can just be reduced to true as where bGameOver is false

false == true  || false == false
     false     ||      true
              true

And where it is true:

true  == true  || true  == false 
     true      ||     false
              true

Privacy & Terms