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.
A requirement becomes an inspectable decision model
Translate prose into atomic claims before writing branch syntax.
- REQUIREMENTstate the rule in ordinary languagesplit
- ATOMIC CLAIMSidentify true or false factscombine
- BOOLEAN EXPRESSIONencode the rule with comparisons and operatorsevaluate
- TRUE / FALSEproduce one boolean resultselect
- 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 enabledE= emergency stop active- result = autonomous allowed
Build the four rows before writing code.
| F | E | allowed |
|---|---|---|
| false | false | ? |
| false | true | ? |
| true | false | ? |
| true | true | ? |
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:
- name the atomic boolean claims;
- write a truth table or representative cases;
- write the Java expression;
- 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.