WEBVTT

00:00:00.000 --> 00:00:02.000


00:00:02.000 --> 00:00:08.866
As another example, let's write a function that determines if a number is a multiple of a factor.

00:00:08.866 --> 00:00:11.466
We can use the remainder function.

00:00:11.466 --> 00:00:18.532
However, if the factor is zero, we will see an error message telling us not to divide by zero.

00:00:18.533 --> 00:00:25.966
We want to return the answer false if factor is zero, so we can add another condition checking that factor is non-zero.

00:00:25.966 --> 00:00:32.566
By putting that condition first, we can rely on short-cut evaluation.

00:00:32.566 --> 00:00:35.799
To trace the function on inputs 10 and 5,

00:00:35.800 --> 00:00:42.566
we first substitute 10 in for number and 5 in for factor in the expression returned by the function.

00:00:42.566 --> 00:00:46.366
We now start to evaluate the expression from left to right.

00:00:46.366 --> 00:00:52.899
The first smaller expression is true, so we can substitute the value true for the first expression.

00:00:52.900 --> 00:00:56.000
Can we use short-cut evaluation to stop early? 

00:00:56.000 --> 00:01:03.166
No, since we are using "and", we can only stop early if have a smaller expression that evaluates to false.

00:01:03.166 --> 00:01:10.399
So now we evaluate the expression 10 remainder 5 to obtain 0, which we place instead of 10 remainder 5 in the expression.

00:01:10.400 --> 00:01:15.166
This results in the expression 0 equals 0, which evaluates to true.

00:01:15.166 --> 00:01:20.166
Finally, the expression "True" and "True" is evaluated to obtain "True".

00:01:20.166 --> 00:01:26.366
On the other hand, on inputs 10 and 0, the first expression evaluates to False.

00:01:26.366 --> 00:01:33.599
Since we are using short-cut evalution, we can now conclude that the value of the entire expression is "False".

00:01:33.600 --> 00:01:39.266
This not only saved time, but saved us from the program failing due to an error message.

00:01:39.266 --> 00:01:45.899
That is, since we never evaluated the second smaller expression, we didn't try to divide by zero.

00:01:45.900 --> 00:01:51.033
Notice that the only time we would divide by zero would be if the second input were zero.

00:01:51.033 --> 00:01:54.466
But that is exactly when we would get to stop early.

00:01:54.466 --> 00:02:01.999
Short-cut evaluation ensures that we never see an error message, no matter what integers the inputs are.

00:02:02.000 --> 00:02:09.966
We can also take advantage of short-cut evaluation in writing a function to determine whether the first character in a string is the letter "a".

00:02:09.966 --> 00:02:18.166
This function seems fine except that trying to find the first character of an empty string will result in an error message.

00:02:18.166 --> 00:02:24.299
We want to return false in that case anyway, so we can add a condition that will be false for the empty string.

00:02:24.300 --> 00:02:30.600
Again, we put the condition first to take advantage of short-cut evaluation.

00:02:30.600 --> 00:02:39.200
As in the previous example, we can trace the behaviour of our function by placing the input value everywhere in the expression returned by the function.

00:02:39.200 --> 00:02:48.066
Moving from left to right, we first evaluate the length of the string "dog" to obtain 3, and then compare 3 and 0 to obtain "True".

00:02:48.066 --> 00:02:52.799
We don't get to stop early, since we only get to stop early when we see "False".

00:02:52.800 --> 00:02:59.000
We next determine the first character in "dog",
and then compare it to "a" to obtain "False".

00:02:59.000 --> 00:03:04.966
This means that the entire expression is false, since the value of "True and False" is False.

00:03:04.966 --> 00:03:12.866
When the input is the empty string, the length is 0, and hence the value of the first expression is "False".

00:03:12.866 --> 00:03:15.866
By short-cut evaluation, we get to stop early,

00:03:15.866 --> 00:03:20.566
and returns "False" without ever trying to find the first character in the string.

00:03:20.566 --> 00:03:29.766
This is very good, because it meant that we avoided applying the index 0 to an empty string, which would have resulted in an error message.

00:03:29.766 --> 00:03:37.499
Again, short-cut evaluation saved the day: it is only when there is an empty string that the first expression will evaluate to false, 

00:03:37.500 --> 00:03:44.600
and that is precisely when we want to stop evaluating expressions before hitting the second one.

00:03:44.600 --> 00:03:49.766
Keep short-cut evaluation in mind to avoid errors such as division by zero,

00:03:49.766 --> 00:03:52.799
extracting a character from an empty string,

00:03:52.800 --> 00:03:55.433
and adding non-numbers.

00:03:55.433 --> 00:04:04.999
Judicious ordering of conditions can allow you to take advantage of short-cut evaluation to circumvent these problems.

