You already know the Boolean type:
booland its two values:
true
falseA Boolean expression is an expression that C# evaluates as a Boolean value.
That means its result is:
trueor:
falseSuppose the Player class contains:
public bool isAvailable;Then this expression:
player1.isAvailablealready produces a Boolean value.
If the field currently contains true, the expression evaluates to true.
If it currently contains false, the expression evaluates to false.
Consider:
public void SetAvailability(bool available)
{
this.isAvailable = available;
}Inside the method:
availableis a Boolean parameter.
Because its type is bool, using:
availableas an expression produces the Boolean value supplied by the caller.
For example:
player1.SetAvailability(true);means that during the call:
available = trueA simple decision can use an existing Boolean expression:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}The condition:
player1.isAvailablemust evaluate to true or false.
The if statement uses that result to choose whether its block runs.
In:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}the Boolean expression is:
player1.isAvailableThe if statement is the decision structure that uses the expression.
These are Boolean values:
true
falseThey can appear where a Boolean value is required.
For example:
player1.isAvailable = true;A name such as:
isAvailableis easier to interpret than a vague name such as:
flagRead:
player1.isAvailableas:
Then true means that statement is true and false means it is false.
Suppose the debugger shows:
player1.isAvailable = falseAt:
if (player1.isAvailable)you can predict that the condition evaluates to false.
The if block will therefore be skipped.
This connects:
Later Learning Activities teach equality comparisons, relational operators, string comparisons, null checks, and the ! operator.
For now, build the core idea: