What the code is supposed to do
PrintOddNumbers(int n) should print the odd numbers below n, one per line. Called with 8, it prints 1, 3, 5 and 7. The starter code runs without any error, but it prints nothing at all, which makes the bug easy to miss: there is no message to read, only an empty output.
public void PrintOddNumbers(int n) {
for (int i = 0; i < n; i++) {
if (n % 2 == 1) {
Console.WriteLine(n);
}
}
}
What goes wrong
The loop header is fine. The variable i counts 0, 1, 2 and so on up to 7. The trouble is in the two lines inside the loop, which use n instead of i.
nis the method's parameter. It is 8 when the method starts and nothing ever changes it, so it is 8 on every pass.n % 2 == 1asks whether 8 is odd. The remainder of 8 divided by 2 is 0, so the answer is false on every pass, andConsole.WriteLinenever runs.
Even if the check were fixed and the print line were not, the output would be wrong in a different way: the method would print 8 over and over instead of the odd numbers. Both lines need the loop variable.
Fixing it step by step
- Find the value that changes. The loop variable
itakes a new value on each pass. The parameternis only the upper limit. - Fix the check. Test
i % 2 == 1, so the condition asks about the current number. - Fix the print. Print
i, notn. Otherwise the check and the output would talk about different numbers.
The fixed code in C#
public void PrintOddNumbers(int n) {
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
Console.WriteLine(i);
}
}
}
public void MainFunction() {
PrintOddNumbers(8);
}
For n = 8, i runs from 0 to 7. The check passes for 1, 3, 5 and 7, and the output is:
1
3
5
7
A few details about this code:
- The condition is
i < n. The loop stops before reachingn, sonitself is never printed, even when it is odd.PrintOddNumbers(9)prints 1, 3, 5 and 7, not 9. %is the remainder operator.i % 2is 0 for even numbers and 1 for odd ones, as long asiis not negative. Hereistarts at 0 and only grows, so== 1is safe. For negative values the remainder keeps the sign of the left operand:-3 % 2is-1, so a check like== 1would miss negative odd numbers. In code that must handle them,x % 2 != 0is the safer test.- Method names are PascalCase. C# is case-sensitive, so
PrintOddNumbersandprintOddNumbersare different names. Use the one the lesson's code declares.
Tracing the fix
Tracing a few passes by hand is the quickest way to confirm a loop does what you intend. With n = 8:
i = 0: 0 % 2 is 0, so nothing is printed.i = 1: 1 % 2 is 1, so 1 is printed.i = 2: 2 % 2 is 0, so nothing is printed.i = 3: 3 % 2 is 1, so 3 is printed.
The pattern repeats up to i = 7. The same trace on the buggy version shows 8 % 2 being 0 on every pass.
Common mistakes
- Using the parameter inside the loop. It compiles, so the compiler can't warn you. The habit that helps: inside a loop, ask which variable changes on each pass.
- Fixing only one of the two lines. Changing the check to
ibut still printingngives 8 printed four times.
Checking your fix
Run the code in the editor above. The output should be exactly four lines: 1, 3, 5, 7. Nothing printed means the condition is still false on every pass. Output starting with 0 means the check was changed to == 0.