If statements in for loops question

Hey Everyone,

I have a question about my code below. Why doesn't month reset to 1 once month becomes greater than 12? I keep getting 13... Thanks in advance! (problem from line 6 - 9)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 void addDay(int n) {
        int monthMax[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int newDay = day + n;
            if (newDay > monthMax[month-1]){
                for (int i = newDay; i > monthMax[month-1]; ++month) {
                    if (month > 12)
                        month = 1;
                    else
                        month = month;
                   i -= monthMax[month-1];
                   day = i;
                }
            }
            else
                day = newDay;
    }
I think that the problem is that your loop is not correct. Let assume that the current month is equal to 12 and i = 32. Then during the first iteration i will be equal to 32 - monthMax[11] = 1.
In the control statement of the loop month will be increased. So after that month is equal to 13. Then the comparision is performed

i > monthMax[month-1];

that is equivalent to

i > monthMax[12];

However array monthMax has elements from monthMax[0] to monthMax[11]. There is no such element as monthMax[12]. So the behavior of the program is undefined.

You can rewrite your loop the following way



1
2
3
4
5
6
7
                while ( newDay > monthMax[month-1] )
                {
                        newDay -= monthMax[month-1];
                        month = ( month == 12 ) ? 1 : month + 1;
                }

                day = newDay;


Even the variable newDay is not needed. You can use the same variable day for the loop. That is

day += n;


and then the loop follows with day instead of newDay.
Last edited on
1
2
3
                for (int i = newDay; i > monthMax[month-1]; ++month) {
                    if (month > 12)
                        month = 1;


You have to understand the order in which this code is run. For loops work like this:

1) Run initialization (int i = newDay;)
2) Check condition (i > monthMax). If false, exit loop. If true, continue to step 3
3) Run loop body
4) Do the increment (++month)
5) Go to step #2


Notice how i > monthMax is checked immediately after you increment month. You don't wrap month to 1 until the loop body, which happens after the condition is checked. Which means the condition is checked with 'month' being 13, which is very bad because you're stepping out of bounds of your array.


Really, this loop is poorly formed. The initialization and condition are focused on 'i' being the variable that drives the loop.. but the increment modifies 'month' instead of 'i' so it's very weird.

For loops are typically used when you want to run a X times -- and X is typically known. Here you don't really know what X is, so a for loop isn't really suitable.

I would rewrite this with a while loop.
Thank you very much Vlad and Disch for your help! I can see the error I was making very clear now due to your detailed responses.

Thanks again!
Topic archived. No new replies allowed.