Loading...
intermediate
Problem 06 • Step 04
sign
sign classifies a number: return 1 if positive, -1 if negative, 0 if zero.
abs had two cases and one if. sign has three cases. But the language’s if only picks between two branches. How do you handle a third?
The answer: the else branch of an if doesn’t have to be a simple value. It can be any expression — including another if. The first if splits the world in two. The second if splits one of those halves again.
Think about which condition to check first. There’s no single right answer, but some orderings lead to simpler code than others.