SECTION 14 / 17

Tutorial: OR is not always two separate IFs

2 min read

A OR B is true when at least one condition is true, including when both are true. A AND B requires both. An IF with an OR condition runs its body once when the condition is true; it does not run the body twice when both sides are true.

One student can score above 90 in both subjects. OR counts the student once; two independent IF updates count that overlap twice.
One student can score above 90 in both subjects. OR counts the student once; two independent IF updates count that overlap twice.
REAL COURSE DATA · THINK BEFORE EACH STEP

OR versus two independent IFs

Trace the records, compare the methods and watch what changes. Back and Reset let you try again.

Ready

CURRENT RECORD X

Ready to begin

Choose Next step to follow the actual course records.

What we remember

Processed
0

Start with Next step. Predict what changes before reading the explanation.

# Count students, not qualifying subject marks
Count = 0
for each score card X:
    if X.Mathematics > 90 OR X.Physics > 90:
        Count = Count + 1

# Different result: this counts successful subject tests
Count = 0
for each score card X:
    if X.Mathematics > 90:
        Count = Count + 1
    if X.Physics > 90:
        Count = Count + 1
ABA OR BIncrease with ORIncrease with two IFs
falsefalsefalse00
truefalsetrue11
falsetruetrue11
truetruetrue12

In the actual scores, 6 students have Maths > 90 OR Physics > 90. Two independent increments produce 8 because Rahul and Deepika pass both tests. The extra 2 is the overlap. “Greater than 90” excludes exactly 90; do not silently change > to ≥.

You can count once using IF A … ELSE IF B … with the same increment, because B is then used only when A is false. But this structure is wrong if you want two separate subject counters that should both increase when both tests pass. Equivalent conditions alone do not make differently placed updates equivalent.

Try it yourself

A student has Maths 97 and Physics 92. How much do the two programs add?

Show explained answer

The OR program adds 1. The two independent IF program adds 2. This is Rahul’s actual pair of marks.

Try it yourself

When can IF A OR B and two independent IFs with the same increment agree?

Show explained answer

When no processed record can satisfy both A and B, or when the overlap happens to be empty in that dataset. Mutual exclusivity is the general structural reason.

Try it yourself

If both conditions are false, does OR still count once?

Show explained answer

No. The IF body is skipped, so both programs add zero.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑