Start learning free

LESSON 128 OF 347 · LOOPS & CONDITIONALS

Print Positive and Negative in C#: Fixing the Buggy Loop

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes an array nums as argument and for every number in the array:

  • prints "negative" if that number is less than zero

  • or prints the number itself, if the number is greater than or equal to zero

So when we called the function for [2, -12, 0, 4, -5, 3], we expected it to print:

2
negative
0
4
negative
3

but it seems like we made some mistakes because when we run our code, it produces this output:

positive
2
-12
0
positive
4
-5
positive
3

Assignment:

Your task is to fix our loop such that it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · LOOPS & CONDITIONALS

    Buggy Code Print Positive Negative in C#, explained

    What the code is supposed to do

    PrintPositiveAndNegative(int[] nums) goes through an array and prints one line per number. If the number is less than zero, the line is the word negative. Otherwise the line is the number itself, which includes zero. For {2, -12, 0, 4, -5, 3} the expected output is:

    2
    negative
    0
    4
    negative
    3
    

    The starter code prints nine lines instead of six: a stray positive before each positive number, and the negative numbers printed as plain numbers.

    public void PrintPositiveAndNegative(int[] nums) {
        foreach (int num in nums) {
            if (num > 0) {
                Console.WriteLine("positive");
            }
            Console.WriteLine(num);
        }
    }
    

    What goes wrong

    There are two separate problems, and both show in the wrong output.

    1. The condition picks the wrong numbers. num > 0 is true for 2, 4 and 3, so those get an extra line that should not exist. For -12 and -5 the condition is false, so they never get the word negative. The message belongs to the numbers below zero, so the condition should be num < 0, and the text should be "negative".
    2. The last line runs every time. Console.WriteLine(num) sits after the if, not inside a branch, so it runs for every element. Even after the first problem is fixed, a negative number would print negative and then its value. Each number must produce exactly one line, so the two prints need to be alternatives.

    Fixing it step by step

    1. Change the condition to num < 0, so it selects the numbers that need a special message.
    2. Print "negative" inside that branch instead of "positive".
    3. Add an else branch and move Console.WriteLine(num) into it. The else runs exactly when the condition is false, that is, when the number is zero or greater.

    The fixed code in C#

    public void PrintPositiveAndNegative(int[] nums) {
        foreach (int num in nums) {
            if (num < 0) {
                Console.WriteLine("negative");
            } else {
                Console.WriteLine(num);
            }
        }
    }
    
    
    public void MainFunction() {
        PrintPositiveAndNegative(new int[]{2, -12, 0, 4, -5, 3});
    }
    

    Tracing it: 2 is not below zero, so the else prints 2. -12 is below zero, so negative is printed and the else is skipped. 0 is not below zero, so the else prints 0. The remaining numbers follow the same two paths, which gives the six expected lines.

    Some details worth knowing:

    • foreach visits each element of the array in order and gives it to you as num. You can read num but not assign to it inside the loop.
    • Zero goes to the else branch. Because the test is < 0, not <= 0, zero prints as 0, as the task asks.
    • Console.WriteLine accepts both a string and an int. The two branches print different types, which is fine because each overload adds the line break itself.
    • The array literal new int[]{...} creates the array. Arrays in C# have a fixed length, which is why the lesson uses int[] here.

    Common mistakes

    • Using <= 0. Zero would print negative, but zero is not less than zero.
    • Printing the number after the if/else. That prints one extra line for every negative number.
    • Two separate if statements (if (num < 0) and if (num >= 0)). This gives the right output but repeats the test; else says directly that the second case is the opposite of the first.
    • Wrong capitalisation of the word. The output must read exactly negative, in lowercase.

    Checking your fix

    Run the code and compare with the six expected lines. If you still see the word positive, the condition or the text is unchanged. If a negative number appears as a number, the condition is still testing the wrong sign. If you get more than six lines, one print is outside the else.

    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free