WEBVTT

00:00:00.700 --> 00:00:05.991
Our task is to classify three scores and total only the passing results.

00:00:06.141 --> 00:00:13.431
This is a useful size for learning control flow: the input is small enough to check by hand, and the

00:00:13.431 --> 00:00:14.525
decisions are visible.

00:00:14.675 --> 00:00:21.890
We will define one function, loop over a list, skip unwanted values, and finish with a bounded while

00:00:21.890 --> 00:00:22.291
loop.

00:00:22.441 --> 00:00:29.395
The full downloadable program includes definitions before calls, so you can run it as a single file.

00:00:31.550 --> 00:00:34.963
A function gives a reusable operation a name.

00:00:35.113 --> 00:00:42.345
Score is its parameter, so each call can classify a different number without repeating the decision tree.

00:00:42.495 --> 00:00:47.125
Return sends the result back to the caller and ends that function call.

00:00:47.275 --> 00:00:52.757
This function returns a string in every branch, which keeps its behavior predictable.

00:00:52.907 --> 00:00:59.073
Read the thresholds aloud: at least ninety, otherwise at least sixty, otherwise retry.

00:00:59.223 --> 00:01:02.529
The order makes the categories mutually exclusive.

00:01:04.680 --> 00:01:08.797
Boundary values are more revealing than a few random inputs.

00:01:08.947 --> 00:01:15.495
Ninety should enter the first branch; sixty should enter the second; fifty nine should reach the last

00:01:15.495 --> 00:01:15.881
branch.

00:01:16.031 --> 00:01:21.833
If we tested only ninety eight, a mistaken passing threshold could go unnoticed.

00:01:21.983 --> 00:01:26.719
These are small executable checks, not a complete grading system.

00:01:26.869 --> 00:01:34.750
For a real application you would also decide what to do with missing values, non numeric input, and

00:01:34.750 --> 00:01:36.939
scores outside the allowed range.

00:01:39.090 --> 00:01:42.226
The for loop introduces score for each list entry.

00:01:42.376 --> 00:01:49.284
Total lives outside the loop because we need to carry it from one iteration to the next and read it

00:01:49.284 --> 00:01:49.629
afterward.

00:01:49.779 --> 00:01:55.731
Continue skips the rest of the current iteration, so fifty five never reaches the addition.

00:01:55.881 --> 00:01:58.868
The final total is one hundred seventy.

00:01:59.018 --> 00:02:04.138
Notice that classify returns a label, while this loop accumulates a number.

00:02:04.288 --> 00:02:09.536
Keeping those responsibilities separate makes both pieces easier to understand.

00:02:11.690 --> 00:02:13.610
Break ends the loop completely.

00:02:13.760 --> 00:02:16.661
Continue only skips the current iteration.

00:02:16.811 --> 00:02:22.379
With this particular input, break prints the first two scores and stops at the failing one.

00:02:22.529 --> 00:02:28.868
Add another passing score after fifty five to see the difference clearly: continue would still consider

00:02:28.868 --> 00:02:30.849
it, while break would not.

00:02:30.999 --> 00:02:33.901
Choose the control statement from the task meaning.

00:02:34.051 --> 00:02:40.429
A report that needs every passing score should not stop just because one earlier entry failed.

00:02:42.580 --> 00:02:45.716
While checks a condition before each iteration.

00:02:45.866 --> 00:02:52.941
Here the counter starts at zero and moves toward the stopping condition on every pass, so the output is

00:02:52.941 --> 00:02:54.058
one, two, three.

00:02:54.208 --> 00:02:58.624
A loop without that update would keep satisfying the same condition.

00:02:58.774 --> 00:03:04.107
Before writing a while loop, identify what changes and why the loop will end.

00:03:04.257 --> 00:03:10.679
For visiting an existing collection, a for loop often makes that termination behavior simpler to see.

00:03:12.830 --> 00:03:18.313
A closure expresses a small function that we can pass into another operation.

00:03:18.463 --> 00:03:22.004
Here apply calls its callback with the supplied value.

00:03:22.154 --> 00:03:28.085
The closure parameter appears before the arrow, and the expression after it computes the result.

00:03:28.235 --> 00:03:35.637
This example also previews the pipe syntax we will study next: twenty one becomes the input to apply.

00:03:35.787 --> 00:03:39.051
Soon we will pass closures into stream operators.

00:03:39.201 --> 00:03:42.465
Keep each closure focused on a small transformation.

00:03:44.620 --> 00:03:47.585
This version runs, but the logic is wrong.

00:03:47.735 --> 00:03:54.477
A score of ninety eight already satisfies the first branch, so the excellent branch cannot receive it.

00:03:54.627 --> 00:03:58.616
A syntax check cannot infer your intended grading rules.

00:03:58.766 --> 00:04:03.971
Put the more specific high threshold first, then rerun the boundary examples.

00:04:04.121 --> 00:04:11.718
This is why running a program once without an error is not enough: correct syntax and correct answers are

00:04:11.718 --> 00:04:14.916
separate things that require different kinds of checking.

00:04:17.070 --> 00:04:24.366
Extend the input with one hundred and sixty, predict the new total, and add a counter for passing scores.

00:04:24.516 --> 00:04:30.489
Keep the counter update beside the addition so both use the same filtering decision.

00:04:30.639 --> 00:04:33.050
Then test the threshold values again.

00:04:33.200 --> 00:04:40.447
You have now combined function calls, returned values, branching, iteration, and mutable state in one

00:04:40.447 --> 00:04:41.413
small task.

00:04:41.563 --> 00:04:47.153
In the next lesson we will express a sequence of transformations with the pipe operator.

