-4

In the first round, 2 is less than 8. Then 2 plus 5 is calculated. The variable "i" should now have the value 7. At last [res += i;] is calculated. So we calculate 0 plus 7.

Problem: At the Output I do not get the number 9 but where is the error?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testing
{
    class testclass
    {
        static void Main()
        {
            int res = calc(2,8,5); // from = 2; to = 8; step = 5;
            Console.WriteLine(res);
            Console.ReadKey();
        }
        static int calc(int from, int to, int step = 1)
        {
            int res = 0;
            for (int i = from; i < to; i += step)
                res += i;
            return res;
        }
    }
}
Output: 9
1ne3o
  • 49
  • 7
  • 1
    Time to debug: put a *break point* on `res += i;` line and have a look on what's going on – Dmitry Bychenko Jul 21 '21 at 10:27
  • i recommend learning how to use the debugger, stepping through your code, and looking at your variables when you actually use them. then you would very easily see that you first add `i=2`, to res, and _then_ add `i=7` to res, resulting in 9. – Franz Gleichmann Jul 21 '21 at 10:28
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/995714), [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – phuclv Jul 21 '21 at 10:40

2 Answers2

1

Debug your program!

This is easy using the debugger, but you can also do poor-man's debugging using Console.WriteLine. That's easier to show in an answer, so let's do that:

static int calc(int from, int to, int step = 1)
{
    int res = 0;
    for (int i = from; i < to; i += step)
    {
        Console.WriteLine($"Adding {i} to {res} to make {res + i}");
        res += i;
    }
    return res;
}

This gives the output:

Adding 2 to 0 to make 2
Adding 7 to 2 to make 9
9

So on the first loop, i = from and so i is 2, and we add 2 to 0 to get 2. We then increment i by step, which is 5, to get 7.

On the second loop, i is 7, and so we add 7 to 2 to get 9. We then increment i by step again to get 12. 12 is greater than to, which is 8, and so we stop iterating.

canton7
  • 37,633
  • 3
  • 64
  • 77
  • Thank you! My mistake was that I already increased the variable "i" by 5 in the first round. – 1ne3o Jul 21 '21 at 10:38
0

Just think about the logic a bit:

  • res starts out as zero.
  • the first time in to the for loop, i is set to 2
  • next time round the loop, i is incremented by the step size of 5 so i is now 7
  • res += i => res = 2 + 7, so res is now 9.
  • i is incremented by the step size of 5 again, so i is now 12
  • the third time round, the loop condition fails so the for loop is done.
phuzi
  • 12,078
  • 3
  • 26
  • 50
  • On the first loop ist "res" set to 2 and on the second loop is "i" set to 7 not in the first loop. – 1ne3o Jul 21 '21 at 11:22