Comparisons and short-circuit logic
Comparison operators produce Boolean results. Combine them to express business rules such as accepting a positive order quantity only when enough books are available. Short-circuit operators let you check a prerequisite before evaluating a dependent expression.
int requested = 4;int available = 7;bool accepted = requested > 0 && requested <= available;System.Console.WriteLine(accepted);Both comparisons are true; an order of four fits the stock.
Expected output (reasoned, not executed):
TrueCompare quantities and values
Use <, <=, >, >=, == and != to compare numeric operands. Equality is a question, not an assignment: == compares, = stores. For strings, ordinary == compares text values case-sensitively; reference equality for general objects is a later topic. C# conditions require bool, not an integer interpreted as true.
Combine conditions
&& requires both conditions to be true. || accepts either or both. ! reverses a Boolean. For bool operands, & and | evaluate both sides; && skips the right side when the left is false, and || skips it when the left is true. ^ is true when exactly one bool operand is true.
Guard a dependent operation
Check that a divisor is nonzero before evaluating an integer division. Put the guard on the left of &&. && binds more tightly than ||; use parentheses for mixed conditions. A guard only protects the dependent operation when it is actually on a skipped path.
Examples and walkthroughs
int books = 12;int shelves = 0;bool fits = shelves != 0 && books / shelves <= 4;System.Console.WriteLine(fits);The first comparison is false. Division is never evaluated.
Expected output (reasoned, not executed):
Falsestring category = "local";bool member = false;System.Console.WriteLine(category == "Local");System.Console.WriteLine(member || category == "local");System.Console.WriteLine(!member);Text case matters; the second condition succeeds because the category matches exactly.
Expected output (reasoned, not executed):
FalseTrueTrueCommon mistake and correction
Never replace a safety guard using && with &. A zero divisor would then still be evaluated. Avoid 0 < requested <= available; combine two complete comparisons instead.
Try it yourself
Write a Boolean rule that accepts a request only if it is positive and at most 8. Predict results for 0, 8 and 9. Write a second rule that safely tests whether 24 divided by a nonzero shelf count is at most 6.
Next: Conversions and checked arithmetic. These examples target C# 14 / net10.0 and are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Comparisons and short-circuit logic — knowledge check
Apply the lesson concepts to the following scenarios.
Which expression tests a positive count no greater than 8?
When does false && right evaluate right?
Which operator evaluates both bool operands?
What is !true?
Which safely checks whether 10 / n is above 2?