As another example, let's write a function that determines if a number is a multiple of a factor.
We can use the remainder function.
However, if the factor is zero, we will see an error message telling us not to divide by zero.
We want to return the answer false if factor is zero, so we can add another condition checking that factor is non-zero.
By putting that condition first, we can rely on short-cut evaluation.
To trace the function on inputs 10 and 5,
we first substitute 10 in for number and 5 in for factor in the expression returned by the function.
We now start to evaluate the expression from left to right.
The first smaller expression is true, so we can substitute the value true for the first expression.
Can we use short-cut evaluation to stop early?
No, since we are using "and", we can only stop early if have a smaller expression that evaluates to false.
So now we evaluate the expression 10 remainder 5 to obtain 0, which we place instead of 10 remainder 5 in the expression.
This results in the expression 0 equals 0, which evaluates to true.
Finally, the expression "True" and "True" is evaluated to obtain "True".
On the other hand, on inputs 10 and 0, the first expression evaluates to False.
Since we are using short-cut evalution, we can now conclude that the value of the entire expression is "False".
This not only saved time, but saved us from the program failing due to an error message.
That is, since we never evaluated the second smaller expression, we didn't try to divide by zero.
Notice that the only time we would divide by zero would be if the second input were zero.
But that is exactly when we would get to stop early.
Short-cut evaluation ensures that we never see an error message, no matter what integers the inputs are.
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".
This function seems fine except that trying to find the first character of an empty string will result in an error message.
We want to return false in that case anyway, so we can add a condition that will be false for the empty string.
Again, we put the condition first to take advantage of short-cut evaluation.
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.
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".
We don't get to stop early, since we only get to stop early when we see "False".
We next determine the first character in "dog", and then compare it to "a" to obtain "False".
This means that the entire expression is false, since the value of "True and False" is False.
When the input is the empty string, the length is 0, and hence the value of the first expression is "False".
By short-cut evaluation, we get to stop early,
and returns "False" without ever trying to find the first character in the string.
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.
Again, short-cut evaluation saved the day: it is only when there is an empty string that the first expression will evaluate to false,
and that is precisely when we want to stop evaluating expressions before hitting the second one.
Keep short-cut evaluation in mind to avoid errors such as division by zero,
extracting a character from an empty string,
and adding non-numbers.
Judicious ordering of conditions can allow you to take advantage of short-cut evaluation to circumvent these problems.