Unit 04 · lesson

A Condition Is a Claim That Must Become True or False

Suppose a requirement says:

A student may enter the lab when the safety agreement is complete and the student has teacher approval.

The important work happens before Java syntax. Identify the claims:

agreement complete?
teacher approval?

Both must be true.

Concept flow

A requirement becomes an inspectable decision model

Translate prose into atomic claims before writing branch syntax.

  1. REQUIREMENTstate the rule in ordinary language
    split
  2. ATOMIC CLAIMSidentify true or false facts
    combine
  3. BOOLEAN EXPRESSIONencode the rule with comparisons and operators
    evaluate
  4. TRUE / FALSEproduce one boolean result
    select
  5. BRANCHexecute behavior that matches the verified rule
boolean agreementComplete = true;
boolean teacherApproved = false;

boolean mayEnter = agreementComplete && teacherApproved;

mayEnter now represents the combined rule.

Comparison operators create boolean values

int score = 82;

boolean passed = score >= 70;
boolean perfect = score == 100;
boolean belowZero = score < 0;

Do not read these as commands. Each expression asks a question and produces true or false.

That makes a useful debugging move possible: print the condition itself.

IO.println(score >= 70);

If a branch surprises you, inspect the boolean claim before rewriting the branch body.

Turn prose into a truth table

Requirement:

A robot may enable autonomous mode only when the field is enabled and the emergency stop is not active.

Let:

  • F = field enabled
  • E = emergency stop active
  • result = autonomous allowed

Build the four rows before writing code.

FEallowed
falsefalse?
falsetrue?
truefalse?
truetrue?

The implementation should follow the table:

boolean allowed = fieldEnabled && !emergencyStop;

If you cannot explain !emergencyStop, rewrite the variable or the prose until the logic becomes readable. Clever boolean expressions are not a virtue when nobody can verify them.

AND, OR, NOT

&& requires both operands to be true.

|| requires at least one operand to be true.

! negates a boolean.

Consider eligibility for a fictional support session:

Attend if you failed the checkpoint OR requested extra practice.

boolean attend = failedCheckpoint || requestedPractice;

Do not substitute && because it "sounds stricter." The operator has to match the requirement.

Parentheses expose grouping

boolean access = staff || student && approved;

Java has precedence rules, but a reviewer should not have to reconstruct your intent mentally.

boolean access = staff || (student && approved);

Now the rule is visible.

Requirement-to-code drill

For each rule:

  1. name the atomic boolean claims;
  2. write a truth table or representative cases;
  3. write the Java expression;
  4. identify one boundary case.

Rules:

  • A score is valid from 0 through 100 inclusive.
  • A user can reset a device if they are an administrator or if they own the device and maintenance mode is enabled.
  • A sensor warning appears when temperature is above 80 or below -10.
  • A command is accepted only when it is nonblank and the system is ready.

Evidence

Choose one rule and create a small Java program that prints the input state, the boolean expression result, and the final decision. Test at least four cases chosen to distinguish the operators from one another.

A single passing case cannot prove a boolean rule.