Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Logical patterns (and, or, not) in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Logical patterns (and, or, not)
Start
↓
Evaluate Left Condition
↓
Evaluate Right Condition
↓
Apply Logical Operator
↓
Result: True or False
↓
Use Result in if/else or assignment
↓
End
Logical patterns combine conditions using AND, OR, and NOT to produce a true or false result used in decisions.
Execution Sample
C Sharp (C#)
bool a = true;
bool b = false;
bool result = a && !b;
Console.WriteLine(result);
This code checks if 'a' is true AND 'b' is NOT true, then prints the result.
Execution Table
StepExpressionEvaluationResult
1atruetrue
2bfalsefalse
3!bnot falsetrue
4a && !btrue && truetrue
5Console.WriteLine(result)printstrue
💡 All expressions evaluated, final result is true, printed to console.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aunassignedtruetruetruetrue
bunassignedunassignedfalsefalsefalse
resultunassignedunassignedunassignedunassignedtrue
Key Moments - 2 Insights
Why does '!b' become true when b is false?
The '!' operator flips the value: if b is false, '!b' is true, as shown in step 3 of the execution_table.
Why is the final result true when 'a' is true and 'b' is false?
Because the expression is 'a && !b', which means 'true AND true' (since !b is true), resulting in true (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of '!b' at step 3?
Afalse
Bnull
Ctrue
Dundefined
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 3 in the execution_table.
At which step does the expression 'a && !b' get evaluated?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the row where the full expression 'a && !b' is evaluated in the execution_table.
If 'a' was false, what would be the result of 'a && !b'?
Atrue
Bfalse
Cnull
Dtrue only if b is true
💡 Hint
Recall that 'AND' requires both sides true; check variable_tracker for 'a' values.
Concept Snapshot
Logical patterns combine conditions:
- AND (&&): true if both true
- OR (||): true if any true
- NOT (!): flips true/false
Use to control decisions based on multiple conditions.
Full Transcript
This lesson shows how logical patterns work in C#. We start by checking each condition: 'a' is true, 'b' is false. Then we apply NOT to 'b', turning false into true. Next, we combine 'a' and '!b' with AND, which is true only if both are true. Since both are true, the final result is true. This result is printed. Understanding how NOT flips values and how AND requires both true helps you write clear decisions in code.

Practice

(1/5)
1.

Which logical operator in C# means both conditions must be true?

and, or, or not?
easy
A. and
B. or
C. not
D. xor

Solution

  1. Step 1: Understand the meaning of logical operators

    The and operator requires both conditions to be true for the whole expression to be true.
  2. Step 2: Compare with other operators

    or means one or both conditions are true, not negates a condition, and xor means exactly one condition is true.
  3. Final Answer:

    and -> Option A
  4. Quick Check:

    and means both true [OK]
Hint: Remember: and = both true, or = any true, not = reverse [OK]
Common Mistakes:
  • Confusing 'and' with 'or'
  • Thinking 'not' means both true
  • Mixing 'xor' with 'and'
2.

Which of these is the correct C# syntax for the logical NOT operator?

?
easy
A. not x
B. x!
C. !x
D. ~x

Solution

  1. Step 1: Recall C# logical NOT syntax

    In C#, the logical NOT operator is ! placed before the variable or expression.
  2. Step 2: Check other options

    not x is not valid C# syntax, ~x is bitwise NOT, and x! is invalid.
  3. Final Answer:

    !x -> Option C
  4. Quick Check:

    Logical NOT uses ! before expression [OK]
Hint: Logical NOT is always ! before the variable [OK]
Common Mistakes:
  • Using 'not' keyword instead of !
  • Confusing bitwise ~ with logical !
  • Placing ! after the variable
3.

What is the output of this C# code?

bool a = true;
bool b = false;
if (a && !b) {
    Console.WriteLine("Yes");
} else {
    Console.WriteLine("No");
}
medium
A. No
B. Yes
C. True
D. False

Solution

  1. Step 1: Evaluate variables and conditions

    Variable a is true, b is false, so !b is true.
  2. Step 2: Evaluate the if condition

    a && !b becomes true && true, which is true, so the if block runs.
  3. Final Answer:

    Yes -> Option B
  4. Quick Check:

    true and not false = true [OK]
Hint: Remember ! reverses boolean, so !false = true [OK]
Common Mistakes:
  • Ignoring the ! operator
  • Thinking false && true is true
  • Confusing output strings with boolean values
4.

Find the error in this C# code snippet:

bool x = true;
if (x && !x) {
    Console.WriteLine("True");
} else {
    Console.WriteLine("False");
}
medium
A. Using single & instead of &&
B. Missing semicolon after if statement
C. Incorrect use of Console.WriteLine
D. Contradictory condition: x and not x can never be true

Solution

  1. Step 1: Analyze the condition logic

    The condition x && !x means x is true AND x is false at the same time, which is impossible.
  2. Step 2: Check syntax correctness

    The syntax is correct: && is used properly, semicolons and Console.WriteLine are fine.
  3. Final Answer:

    Contradictory condition: x and not x can never be true -> Option D
  4. Quick Check:

    True and not true = false always [OK]
Hint: Check if condition can ever be true logically [OK]
Common Mistakes:
  • Thinking condition can be true
  • Confusing & with &&
  • Looking for syntax errors when logic is the issue
5.

You want to write a C# switch expression that returns "Valid" only if age is between 18 and 30 inclusive, and hasID is true. Which pattern correctly uses logical operators?

return (age, hasID) switch {
    ??? => "Valid",
    _ => "Invalid"
};
hard
A. var (a, id) when a >= 18 && a <= 30 && id
B. (>= 18 and <= 30 and true, hasID)
C. var a when a >= 18 && a <= 30 && hasID == true
D. (>= 18 and <= 30, true)

Solution

  1. Step 1: Understand switch pattern matching with tuples

    The switch matches a tuple (age, hasID). To check conditions on both, use a when clause with logical operators.
  2. Step 2: Analyze each option

    var (a, id) when a >= 18 && a <= 30 && id correctly uses tuple deconstruction var (a, id) and checks a >= 18 && a <= 30 && id (where id is boolean). Others are invalid syntax or misuse logical operators.
  3. Final Answer:

    var (a, id) when a >= 18 && a <= 30 && id -> Option A
  4. Quick Check:

    Tuple pattern with when and && is correct [OK]
Hint: Use tuple deconstruction with when and && for multiple conditions [OK]
Common Mistakes:
  • Trying to use and inside tuple without when
  • Misplacing logical operators outside when clause
  • Not deconstructing tuple properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard