Lecture Notes on Boolean Types and Operators

Selection and Boolean Types
Introduction to Selection
  • 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.

Boolean Type
  • Booleans are simple: they can only be truetrue or falsefalse.

  • It's like a light switch: either on (truetrue) or off (falsefalse).

  • Declaration:

    • In C#, you can use System.Boolean\texttt{System.Boolean}

    • Or simply use the bool\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 falsefalse.

  • Examples:

  bool isAtSemesterBreak = false;
  bool areYouBored = true;
  • Literals:

    • When you write truetrue or falsefalse in code, use lowercase.

  • Output:

    • When you print a boolean to the screen, the first letter is capitalized (TrueTrue or FalseFalse).

Boolean Operators
  • These operators help you compare values and get a truetrue or falsefalse result.

  • Less Than Operator: \,<

    • Returns truetrue if the left side is smaller than the right side.

    • Example:
      csharp 5 < 10 // true

  • Greater Than Operator: \,>

    • Returns truetrue if the left side is larger than the right side.

    • Example:
      csharp 10 > 5 // true

  • Less Than or Equal To Operator: \,<=

    • Returns truetrue 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 truetrue 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

Logical Operators
  • These let you combine two boolean expressions.

  • Types: AND, OR, EXCLUSIVE OR (XOR).

  • AND: Returns truetrue only if both things are truetrue.

    • Example: (Is it sunny?) AND (Is it warm?) -> Must be both to be truetrue.

  • OR: Returns truetrue if either one or both things are truetrue.

    • Example: (Is it Saturday?) OR (Is it Sunday?) -> If either is truetrue, then it's the weekend.

  • XOR: Returns truetrue if only one thing is truetrue.

    • Example: (Is the light switch on?) XOR (Is the generator running?) -> If only one is truetrue, 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).

Logical Operators in C

||||
|---|---|---|---|
||||
||||

Short Circuit Evaluation
  • Using double symbols (&amp;&amp;\texttt{\&amp;\&amp;}, ||\texttt{||}) can speed up your code.

  • Double ampersand (&amp;&amp;\texttt{\&amp;\&amp;}):

    • If the first part is falsefalse, the second part isn't even checked because the whole thing will be falsefalse.

    • Example:
      csharp (false) && (expensive calculation) // calculation skipped

  • Double pipe (||\texttt{||}):

    • If the first part is truetrue, the second part isn't checked because the whole thing will be truetrue.

    • 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 (&amp;&amp;\texttt{\&amp;\&amp;}, ||\texttt{||}) for better performance.

  • Parentheses:

    • Use parentheses around boolean expressions to avoid mistakes.

Collecting Boolean Input from User
  • Input: Users can type truetrue or falsefalse (it doesn't matter if it's uppercase or lowercase).

  • Invalid Inputs: Typing yesyes or nono will cause an error.

  • Code Example:
    1