Programming from zero

Find the first server matching a condition

easyLoops

Problem statement

Sometimes you don't need to check every item in a list, you just need the first one that matches. Once it's found, there's no reason to keep looping, break stops the loop immediately instead of wastefully checking everything after it.

Common mistake: forgetting break, so a later non-matching value overwrites the answer.

TEXT
Wrong:
found = None
for time in response_times:
if time > 200:
found = time

Without break, the loop keeps going after finding a match, and if a later value also happens to satisfy the condition, it silently overwrites found, losing the first match, which is what the problem actually asked for.

Your task: given response times [120, 95, 250, 180, 300], find and print the first one over 200ms.

Expected output:

TEXT
250

Approach

Loop through the values, and the moment one is over 200, print it and stop:

response_times = [120, 95, 250, 180, 300]
for time in response_times:
    if time > 200:
        print(time)
        break