Problem overview
You write a method IsNegative that takes an integer n and prints one word to the console. It prints negative when n is less than zero, and positive for everything else, including zero. The method does not return anything: its result is the text it prints.
| n | Printed | Why |
|---|---|---|
| 5 | positive | 5 is not less than 0 |
| 0 | positive | 0 is not less than 0 |
| -2 | negative | -2 is less than 0 |
Zero is the case to watch. Mathematically zero is neither positive nor negative, but this problem puts it in the "positive" group, because the rule is "negative if below zero, otherwise positive".
Approach
There are exactly two outcomes, and one test separates them: is n < 0? If the answer is yes, print negative. If the answer is no, print positive. That is the shape of an if-else statement, so there is no need for a second condition.
Testing for "less than zero" rather than "greater than zero" is what handles zero correctly. Since 0 < 0 is false, zero lands in the else branch automatically.
Compare this with the alternative of two independent checks, one for n < 0 and one for n >= 0. It works, but it asks the same question twice and relies on you keeping the two conditions exactly opposite. With else, the second branch is defined as "whatever the first test rejected", so the two can never drift apart.
Step by step
- Write the condition. Start with
if (n < 0)and an empty block. The parentheses are required, and the condition has to be abool, which a comparison always produces. - Handle the negative case. Inside the block, call
Console.WriteLine("negative");.WriteLineprints the text and ends the line. - Handle the other case. Add
elseafter the closing brace and print"positive"in its block. Theelseruns for everynthat failed the test, which means zero and all positive numbers.
The solution in C#
class Solution {
public void IsNegative(int n) {
if (n < 0) {
Console.WriteLine("negative");
} else {
Console.WriteLine("positive");
}
}
}
C# details
voidin the signature means the method returns no value. Nothing here usesreturn, because the output happens throughConsole.WriteLine.- The method name is
IsNegativewith a capitalI. C# is case-sensitive, so a method calledisNegativewould be a different name, and calling it would fail to compile. - The strings must match exactly, including lowercase letters:
"Negative"is not the same text as"negative". - The type
intonly holds whole numbers, so you never have to think about decimals in this method.
Reading it as the program runs
For n = -2, C# evaluates -2 < 0, gets true, prints negative and jumps past the else block. For n = 0, it evaluates 0 < 0, gets false, skips the first block and prints positive. Tracing a couple of inputs like this is a good habit for checking conditions.
Edge cases and common mistakes
- Zero. Testing
n <= 0would printnegativefor zero, which is wrong here. Testingn > 0forpositiveand everything else fornegativewould also get zero wrong. - Printing twice. If you write two separate
ifstatements and make a mistake in the conditions, both or neither could print. Theif-elseform guarantees exactly one line of output. - Returning instead of printing. The method is
void, soreturn "negative";will not compile. The answer has to go to the console. - Using
=instead of==. This problem needs only<, but if you compare to a specific value later, remember that=assigns and==compares.
Testing
Try these inputs in the editor above:
5printspositive.0printspositive.-2printsnegative.-1printsnegative, the closest negative integer to the boundary.