SECTION 17 / 28

Reset found and leave only the inner search

1 min read
On a match, exitloop ends the nearest enclosing search loop. The outer student loop continues with later students.
On a match, exitloop ends the nearest enclosing search loop. The outer student loop continues with later students.
REAL COURSE DATA · SORT, GROUP AND VERIFY

Watch exitloop leave only the inner search

Follow the selected procedure. Change a field, tie rule or weekday and explain why the result changes.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next change. Keep track of which list level, field and position convention the procedure uses.

Confirm=[]; Reject=[]
foreach x in MathsA:
    found = false
    foreach y in PhysicsA:
        if x == y:
            found = true
            exitloop
    if found: Confirm = Confirm ++ [x]
    else: Reject = Reject ++ [x]

# For MathsB, search PhysicsA, then PhysicsB only if not found.
# Classify x once after the allowed searches finish.

One non-match does not prove absence. Keep searching allowed destinations until a match appears or every candidate is exhausted. True can be established early; false requires exhausting the permitted searches. Reset found = false for EACH new outer x.

exitloop is different from return. It leaves the nearest enclosing loop, then continues the surrounding procedure. After a match in Physics A for a Maths B student, skip the Physics B search. Keep processing all other Maths B students instead of returning from the whole task.

Try it yourself

What goes wrong if found is initialized once before the outer loop?

Show explained answer

A true value from one student may carry over and falsely confirm later students.

Try it yourself

Does exitloop stop every nested loop?

Show explained answer

No. Here it exits the nearest inner foreach only.

Try it yourself

Can you append Reject immediately after one unequal y?

Show explained answer

No. A later eligible y may match x. Decide after the necessary searches.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑