S3U

Science / Programming basics / Grade 7 / sc7s1

First Match: Trace Before You Choose

Find the first value that meets a rule, and distinguish its value from its position and the number of checks.

All worksheets

Watch the lesson | Next: repair a missing stop

5 questions0m 0s
Learn the skill

Keep the supplied order. Start result at 0. Test each value against value >= 2. At the first match, copy the value and stop this scan. Continue with the report after the loop. If nothing matches, keep fallback 0; it is not a found item. Item numbers on this page begin at 1.

Worked example

For [0, 4, 3], inspect 0 (false), then 4 (true). Copy 4 at item 2 and stop. Two comparisons ran. The later 3 was not inspected.

Four positions labeled item 1 through item 4 contain 1, 0, 3 and 4. Their comparison results are left for the learner.
Trace only the comparisons that actually run.

The next delivery list

Use [1, 0, 3, 4] in that exact order. The rule is value >= 2, the starting result is 0, and the report comes after the loop. Do not sort, add all matches, or keep searching after copying the first match.

Question 1 Which result is copied from [1, 0, 3, 4]?
Question 2 How many comparisons run for this list?
Question 3 Which value is not inspected?
Question 4 For a separate list [2, 4], which result and number of comparisons are correct?
Question 5 For a separate list [1, 0], what does result 0 mean?