Problem overview
The method isNegative takes an integer n and prints a word to the console: negative when n is less than zero, and positive otherwise. It doesn't return anything.
Three examples show the rule. For n = 5 it prints positive. For n = -2 it prints negative. For n = 0 it prints positive, because zero is not below zero. The word "positive" here really means "zero or greater", so zero lands on the positive side.
Approach
The question has two possible answers, and the decision depends on one comparison: is n < 0? That is exactly what an if-else statement is for. If the comparison is true, run one block; if it is false, run the other.
The strict < matters. With n <= 0, zero would be reported as negative. With n > 0 as the test for "positive", zero would fall into the other branch. Testing n < 0 and letting else take everything else gives the right answer for all three examples.
Step by step
- Write the
ifwith its condition. The condition isn < 0. Swift needs no parentheses around it, but the braces after it are required. At this stage the body can stay empty. - Print inside the
if. Addprint("negative")between the braces. The quotes make it a string, andprintends the output with a line break. - Add the
else. It has no condition: it runs whenevern < 0is false, which covers zero and every positive number. It prints"positive".
The solution in Swift
class Solution {
func isNegative(_ n: Int) {
if n < 0 {
print("negative")
} else {
print("positive")
}
}
}
Swift details worth knowing:
- The underscore in
_ n: Intmeans the caller doesn't write a label. You callisNegative(5), notisNegative(n: 5). - No return type. The signature has no
->, so the method returns nothing and its only effect is the output. Trying to use its result is pointless:let r = s.isNegative(3)givesrthe empty tuple type(), and the compiler warns that this "may be unexpected". - Calling it. The method belongs to a class, so create an instance first. After
let s = Solution(), the calls.isNegative(-2)printsnegative. Intonly. The parameter is anInt, so the comparison with the literal0needs no conversion. ADoubleargument would not compile.
Edge cases and common mistakes
- Zero.
isNegative(0)printspositive. Using<=by mistake would printnegative. - Printing versus returning. This method prints. If the output has to be a value your code can use later, the method would need a return type, as in the next lessons of this chapter.
- Mismatched words. The output must be exactly
negativeorpositive, in lowercase, with no extra text. - Printing both. Without the
else, writing a secondprint("positive")after theifwould run for negative numbers too. Theelsekeeps the two outcomes exclusive. - A condition that isn't a
Bool. Swift rejects tests likeif n {, so the comparison is always spelled out.
Testing
isNegative(5)printspositive.isNegative(0)printspositive.isNegative(-2)printsnegative.isNegative(-1)printsnegative, the closest negative value to the boundary.isNegative(1)printspositive, the closest positive value.
