Selection lets programs change what they do based on data.
Think of it like a fork in the road: the program chooses which path to take.
A key part of this is the boolean type, which helps the program make these choices.
Booleans are simple: they can only be true or false.
It's like a light switch: either on (true) or off (false).
Declaration:
In C#, you can use \texttt{System.Boolean}
Or simply use the \texttt{bool} keyword.
Storage:
Booleans are stored as a byte.
Technically, they only need one bit, but they use a full byte.
Default Value:
The default value of a boolean is false.
Examples:
bool isAtSemesterBreak = false;
bool areYouBored = true;
Literals:
When you write true or false in code, use lowercase.
Output:
When you print a boolean to the screen, the first letter is capitalized (True or False).
These operators help you compare values and get a true or false result.
Less Than Operator: \,<
Returns true if the left side is smaller than the right side.
Example:csharp 5 < 10 // true
Greater Than Operator: \,>
Returns true if the left side is larger than the right side.
Example:csharp 10 > 5 // true
Less Than or Equal To Operator: \,<=
Returns true if the left side is smaller than or equal to the right side.
Example:csharp 5 <= 5 // true
Greater Than or Equal To Operator: \,>=
Returns true if the left side is larger than or equal to the right side.
Example:csharp 5 >= 5 // true
Equal To Operator (Equivalence Operator): \,==
Checks if both sides are the same.
Important: Use two equal signs (==), not one (=), which is for assignment.
Example:csharp 5 == 5 // true
Not Equal Operator: \,!=
Checks if both sides are different.
Example:csharp 5 != 10 // true
Two-Character Operators: Make sure the characters are right next to each other (like <= , not < = ).
Usage with Variables: Booleans are great with variables.
Example: A grocery delivery service.
A customer spends $80.50.
Free delivery starts at $75.csharp bool freeDelivery = (amountSpent > threshold); // true
These let you combine two boolean expressions.
Types: AND, OR, EXCLUSIVE OR (XOR).
AND: Returns true only if both things are true.
Example: (Is it sunny?) AND (Is it warm?) -> Must be both to be true.
OR: Returns true if either one or both things are true.
Example: (Is it Saturday?) OR (Is it Sunday?) -> If either is true, then it's the weekend.
XOR: Returns true if only one thing is true.
Example: (Is the light switch on?) XOR (Is the generator running?) -> If only one is true, the power is on.
Venn Diagram Analogy:
Imagine Students (red) and Staff (blue) at QUT.
The purple part where they overlap: that's a Tutor who is both a PhD student and staff.
C# Code Representation:
booleanExpression1 BOOLEAN_OPERATOR booleanExpression2
Example: Tutor Status
You must be a student AND a staff member to be a tutor.
Example: Parking Discount
You get a discount if you are a student OR a staff member (or both).
||||
|---|---|---|---|
||||
||||
Using double symbols (\texttt{\&\&}, \texttt{||}) can speed up your code.
Double ampersand (\texttt{\&\&}):
If the first part is false, the second part isn't even checked because the whole thing will be false.
Example:csharp (false) && (expensive calculation) // calculation skipped
Double pipe (\texttt{||}):
If the first part is true, the second part isn't checked because the whole thing will be true.
Example:csharp (true) || (expensive calculation) // calculation skipped
Efficiency:
This can really help if one part takes a long time or happens a lot.
Best Practice: Always use double symbols (\texttt{\&\&}, \texttt{||}) for better performance.
Parentheses:
Use parentheses around boolean expressions to avoid mistakes.
Input: Users can type true or false (it doesn't matter if it's uppercase or lowercase).
Invalid Inputs: Typing yes or no will cause an error.
Code Example:
1