What would cause a for loop to decrement when it's supposed to increment?












5















I wrote a method to calculate how long ago a father was twice as old as his son and in how many years from now this would be true. Unexpectedly, it returns "-2 years ago" for an 8-year-old father and a 3-year-old son. Equally unexpectedly, it returns "-1 years from now" for a 3-year-old father and a 2-year-old son. I am not concerned about how to improve the code because I already know how to do this. Instead, I am puzzled about why the for loop counter appears to be decrementing when it's supposed to increment.



Here is my code.



public class TwiceAsOld {

public static void twiceAsOld (int currentFathersAge, int currentSonsAge) {

int yearsAgo;
int yearsFromNow;
int pastFathersAge = currentFathersAge;
int pastSonsAge = currentSonsAge;
int futureFathersAge = currentFathersAge;
int futureSonsAge = currentSonsAge;

for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge; yearsAgo++) {
pastFathersAge--;
pastSonsAge--;
}

System.out.println("The father was last twice as old as the son " + yearsAgo + " years ago.");

for (yearsFromNow = 0; futureFathersAge != 2 * futureSonsAge; yearsFromNow++) {
futureFathersAge++;
futureSonsAge++;
}

System.out.println("The father will be twice as old as the son in " + yearsFromNow + " years from now.");

}

public static void main(String args) {
twiceAsOld(8, 3);
twiceAsOld(3, 2);
}
}


With twiceAsOld(8, 3), the for loop's increment appears to have reversed itself to count down from 0 instead of up. With twiceAsOld(3, 2), the -1 might stand for an error indicating that the father has never been twice as old as his son and never will be. What I don't understand is what would cause a for loop to start decrementing the i value when it's supposed to increment. I was expecting the counter to increment indefinitely until the program ran out of memory.



I already know how to improve this program, but I am curious about how the counter in a for loop can decrease when it's supposed to increase. Can anybody explain this?



(UPDATE: Thanks everyone for your answers. I can't believe I forgot about integer overflow. I tried making the variables longs instead of integers, but this made the program even slower. Anyway, now I realize that the counter was incrementing all along until it overflew and landed at a negative value.)










share|improve this question




















  • 1





    Since there's no decrement of that value, it is almost certainly an integer overflow.

    – Marc Baumbach
    Jan 3 at 1:57
















5















I wrote a method to calculate how long ago a father was twice as old as his son and in how many years from now this would be true. Unexpectedly, it returns "-2 years ago" for an 8-year-old father and a 3-year-old son. Equally unexpectedly, it returns "-1 years from now" for a 3-year-old father and a 2-year-old son. I am not concerned about how to improve the code because I already know how to do this. Instead, I am puzzled about why the for loop counter appears to be decrementing when it's supposed to increment.



Here is my code.



public class TwiceAsOld {

public static void twiceAsOld (int currentFathersAge, int currentSonsAge) {

int yearsAgo;
int yearsFromNow;
int pastFathersAge = currentFathersAge;
int pastSonsAge = currentSonsAge;
int futureFathersAge = currentFathersAge;
int futureSonsAge = currentSonsAge;

for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge; yearsAgo++) {
pastFathersAge--;
pastSonsAge--;
}

System.out.println("The father was last twice as old as the son " + yearsAgo + " years ago.");

for (yearsFromNow = 0; futureFathersAge != 2 * futureSonsAge; yearsFromNow++) {
futureFathersAge++;
futureSonsAge++;
}

System.out.println("The father will be twice as old as the son in " + yearsFromNow + " years from now.");

}

public static void main(String args) {
twiceAsOld(8, 3);
twiceAsOld(3, 2);
}
}


With twiceAsOld(8, 3), the for loop's increment appears to have reversed itself to count down from 0 instead of up. With twiceAsOld(3, 2), the -1 might stand for an error indicating that the father has never been twice as old as his son and never will be. What I don't understand is what would cause a for loop to start decrementing the i value when it's supposed to increment. I was expecting the counter to increment indefinitely until the program ran out of memory.



I already know how to improve this program, but I am curious about how the counter in a for loop can decrease when it's supposed to increase. Can anybody explain this?



(UPDATE: Thanks everyone for your answers. I can't believe I forgot about integer overflow. I tried making the variables longs instead of integers, but this made the program even slower. Anyway, now I realize that the counter was incrementing all along until it overflew and landed at a negative value.)










share|improve this question




















  • 1





    Since there's no decrement of that value, it is almost certainly an integer overflow.

    – Marc Baumbach
    Jan 3 at 1:57














5












5








5


1






I wrote a method to calculate how long ago a father was twice as old as his son and in how many years from now this would be true. Unexpectedly, it returns "-2 years ago" for an 8-year-old father and a 3-year-old son. Equally unexpectedly, it returns "-1 years from now" for a 3-year-old father and a 2-year-old son. I am not concerned about how to improve the code because I already know how to do this. Instead, I am puzzled about why the for loop counter appears to be decrementing when it's supposed to increment.



Here is my code.



public class TwiceAsOld {

public static void twiceAsOld (int currentFathersAge, int currentSonsAge) {

int yearsAgo;
int yearsFromNow;
int pastFathersAge = currentFathersAge;
int pastSonsAge = currentSonsAge;
int futureFathersAge = currentFathersAge;
int futureSonsAge = currentSonsAge;

for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge; yearsAgo++) {
pastFathersAge--;
pastSonsAge--;
}

System.out.println("The father was last twice as old as the son " + yearsAgo + " years ago.");

for (yearsFromNow = 0; futureFathersAge != 2 * futureSonsAge; yearsFromNow++) {
futureFathersAge++;
futureSonsAge++;
}

System.out.println("The father will be twice as old as the son in " + yearsFromNow + " years from now.");

}

public static void main(String args) {
twiceAsOld(8, 3);
twiceAsOld(3, 2);
}
}


With twiceAsOld(8, 3), the for loop's increment appears to have reversed itself to count down from 0 instead of up. With twiceAsOld(3, 2), the -1 might stand for an error indicating that the father has never been twice as old as his son and never will be. What I don't understand is what would cause a for loop to start decrementing the i value when it's supposed to increment. I was expecting the counter to increment indefinitely until the program ran out of memory.



I already know how to improve this program, but I am curious about how the counter in a for loop can decrease when it's supposed to increase. Can anybody explain this?



(UPDATE: Thanks everyone for your answers. I can't believe I forgot about integer overflow. I tried making the variables longs instead of integers, but this made the program even slower. Anyway, now I realize that the counter was incrementing all along until it overflew and landed at a negative value.)










share|improve this question
















I wrote a method to calculate how long ago a father was twice as old as his son and in how many years from now this would be true. Unexpectedly, it returns "-2 years ago" for an 8-year-old father and a 3-year-old son. Equally unexpectedly, it returns "-1 years from now" for a 3-year-old father and a 2-year-old son. I am not concerned about how to improve the code because I already know how to do this. Instead, I am puzzled about why the for loop counter appears to be decrementing when it's supposed to increment.



Here is my code.



public class TwiceAsOld {

public static void twiceAsOld (int currentFathersAge, int currentSonsAge) {

int yearsAgo;
int yearsFromNow;
int pastFathersAge = currentFathersAge;
int pastSonsAge = currentSonsAge;
int futureFathersAge = currentFathersAge;
int futureSonsAge = currentSonsAge;

for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge; yearsAgo++) {
pastFathersAge--;
pastSonsAge--;
}

System.out.println("The father was last twice as old as the son " + yearsAgo + " years ago.");

for (yearsFromNow = 0; futureFathersAge != 2 * futureSonsAge; yearsFromNow++) {
futureFathersAge++;
futureSonsAge++;
}

System.out.println("The father will be twice as old as the son in " + yearsFromNow + " years from now.");

}

public static void main(String args) {
twiceAsOld(8, 3);
twiceAsOld(3, 2);
}
}


With twiceAsOld(8, 3), the for loop's increment appears to have reversed itself to count down from 0 instead of up. With twiceAsOld(3, 2), the -1 might stand for an error indicating that the father has never been twice as old as his son and never will be. What I don't understand is what would cause a for loop to start decrementing the i value when it's supposed to increment. I was expecting the counter to increment indefinitely until the program ran out of memory.



I already know how to improve this program, but I am curious about how the counter in a for loop can decrease when it's supposed to increase. Can anybody explain this?



(UPDATE: Thanks everyone for your answers. I can't believe I forgot about integer overflow. I tried making the variables longs instead of integers, but this made the program even slower. Anyway, now I realize that the counter was incrementing all along until it overflew and landed at a negative value.)







java loops for-loop increment decrement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 2:39







K Man

















asked Jan 3 at 1:50









K ManK Man

751313




751313








  • 1





    Since there's no decrement of that value, it is almost certainly an integer overflow.

    – Marc Baumbach
    Jan 3 at 1:57














  • 1





    Since there's no decrement of that value, it is almost certainly an integer overflow.

    – Marc Baumbach
    Jan 3 at 1:57








1




1





Since there's no decrement of that value, it is almost certainly an integer overflow.

– Marc Baumbach
Jan 3 at 1:57





Since there's no decrement of that value, it is almost certainly an integer overflow.

– Marc Baumbach
Jan 3 at 1:57












3 Answers
3






active

oldest

votes


















4














It became negative because that is what happens in Java when an int calculation overflows.



Take a look at
https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2



It says that




If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.







share|improve this answer































    3














    Didn't you notice that your program runs quite slowly? :)



    For the (8, 3) years ago case, your for loop keeps looping and looping, trying to find a year that the father is twice as old, but as we know, the father will only become twice as old in the future, but not in the past. The for loop doesn't know this and it will try very hard to find such a year. It tries so hard that yearsAgo is incremented past the max value of int. This causes an overflow, and the value of yearsAgo will "wrap back around" to the minimum value of int, which is a negative number. And then this negative number will get incremented many many times, until -2.



    The same goes for the other case.



    To fix this, you can add if statements to check if the results are negative:



    public static void twiceAsOld (int currentFathersAge, int currentSonsAge) {

    int yearsAgo;
    int yearsFromNow;
    int pastFathersAge = currentFathersAge;
    int pastSonsAge = currentSonsAge;
    int futureFathersAge = currentFathersAge;
    int futureSonsAge = currentSonsAge;


    for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge; yearsAgo++) {

    pastFathersAge--;
    pastSonsAge--;
    }

    // Here!
    if (yearsAgo >= 0) {
    System.out.println("The father was last twice as old as the son " + yearsAgo + " years ago.");
    }

    for (yearsFromNow = 0; futureFathersAge != 2 * futureSonsAge; yearsFromNow++) {
    futureFathersAge++;
    futureSonsAge++;
    }

    if (yearsFromNow >= 0) {
    System.out.println("The father will be twice as old as the son in " + yearsFromNow + " years from now.");
    }

    }


    You can also stop the loop when it reaches negative values to make your program faster:



    for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge && yearsAgo >= 0; yearsAgo++) {





    share|improve this answer































      1














      When I debug your code I can see that yearsAgo is incrementing without bound, causing pastFathersAge and pastSonsAge to go into negatives. This is causing negative integer overflow. This happens because your condition pastFathersAge != 2 * pastSonsAge is never met (rather, never NOT met). Not until your futureFathersAge has gone all the way through the negatives, back into positives, and finally settles on -2.



      The moral of the story is to make certain that your terminating condition for your loop can always can be met. Don't use !=, use >= or <= instead.






      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015399%2fwhat-would-cause-a-for-loop-to-decrement-when-its-supposed-to-increment%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        It became negative because that is what happens in Java when an int calculation overflows.



        Take a look at
        https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2



        It says that




        If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.







        share|improve this answer




























          4














          It became negative because that is what happens in Java when an int calculation overflows.



          Take a look at
          https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2



          It says that




          If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.







          share|improve this answer


























            4












            4








            4







            It became negative because that is what happens in Java when an int calculation overflows.



            Take a look at
            https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2



            It says that




            If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.







            share|improve this answer













            It became negative because that is what happens in Java when an int calculation overflows.



            Take a look at
            https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2



            It says that




            If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 2:01









            mkjhmkjh

            1,1501023




            1,1501023

























                3














                Didn't you notice that your program runs quite slowly? :)



                For the (8, 3) years ago case, your for loop keeps looping and looping, trying to find a year that the father is twice as old, but as we know, the father will only become twice as old in the future, but not in the past. The for loop doesn't know this and it will try very hard to find such a year. It tries so hard that yearsAgo is incremented past the max value of int. This causes an overflow, and the value of yearsAgo will "wrap back around" to the minimum value of int, which is a negative number. And then this negative number will get incremented many many times, until -2.



                The same goes for the other case.



                To fix this, you can add if statements to check if the results are negative:



                public static void twiceAsOld (int currentFathersAge, int currentSonsAge) {

                int yearsAgo;
                int yearsFromNow;
                int pastFathersAge = currentFathersAge;
                int pastSonsAge = currentSonsAge;
                int futureFathersAge = currentFathersAge;
                int futureSonsAge = currentSonsAge;


                for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge; yearsAgo++) {

                pastFathersAge--;
                pastSonsAge--;
                }

                // Here!
                if (yearsAgo >= 0) {
                System.out.println("The father was last twice as old as the son " + yearsAgo + " years ago.");
                }

                for (yearsFromNow = 0; futureFathersAge != 2 * futureSonsAge; yearsFromNow++) {
                futureFathersAge++;
                futureSonsAge++;
                }

                if (yearsFromNow >= 0) {
                System.out.println("The father will be twice as old as the son in " + yearsFromNow + " years from now.");
                }

                }


                You can also stop the loop when it reaches negative values to make your program faster:



                for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge && yearsAgo >= 0; yearsAgo++) {





                share|improve this answer




























                  3














                  Didn't you notice that your program runs quite slowly? :)



                  For the (8, 3) years ago case, your for loop keeps looping and looping, trying to find a year that the father is twice as old, but as we know, the father will only become twice as old in the future, but not in the past. The for loop doesn't know this and it will try very hard to find such a year. It tries so hard that yearsAgo is incremented past the max value of int. This causes an overflow, and the value of yearsAgo will "wrap back around" to the minimum value of int, which is a negative number. And then this negative number will get incremented many many times, until -2.



                  The same goes for the other case.



                  To fix this, you can add if statements to check if the results are negative:



                  public static void twiceAsOld (int currentFathersAge, int currentSonsAge) {

                  int yearsAgo;
                  int yearsFromNow;
                  int pastFathersAge = currentFathersAge;
                  int pastSonsAge = currentSonsAge;
                  int futureFathersAge = currentFathersAge;
                  int futureSonsAge = currentSonsAge;


                  for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge; yearsAgo++) {

                  pastFathersAge--;
                  pastSonsAge--;
                  }

                  // Here!
                  if (yearsAgo >= 0) {
                  System.out.println("The father was last twice as old as the son " + yearsAgo + " years ago.");
                  }

                  for (yearsFromNow = 0; futureFathersAge != 2 * futureSonsAge; yearsFromNow++) {
                  futureFathersAge++;
                  futureSonsAge++;
                  }

                  if (yearsFromNow >= 0) {
                  System.out.println("The father will be twice as old as the son in " + yearsFromNow + " years from now.");
                  }

                  }


                  You can also stop the loop when it reaches negative values to make your program faster:



                  for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge && yearsAgo >= 0; yearsAgo++) {





                  share|improve this answer


























                    3












                    3








                    3







                    Didn't you notice that your program runs quite slowly? :)



                    For the (8, 3) years ago case, your for loop keeps looping and looping, trying to find a year that the father is twice as old, but as we know, the father will only become twice as old in the future, but not in the past. The for loop doesn't know this and it will try very hard to find such a year. It tries so hard that yearsAgo is incremented past the max value of int. This causes an overflow, and the value of yearsAgo will "wrap back around" to the minimum value of int, which is a negative number. And then this negative number will get incremented many many times, until -2.



                    The same goes for the other case.



                    To fix this, you can add if statements to check if the results are negative:



                    public static void twiceAsOld (int currentFathersAge, int currentSonsAge) {

                    int yearsAgo;
                    int yearsFromNow;
                    int pastFathersAge = currentFathersAge;
                    int pastSonsAge = currentSonsAge;
                    int futureFathersAge = currentFathersAge;
                    int futureSonsAge = currentSonsAge;


                    for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge; yearsAgo++) {

                    pastFathersAge--;
                    pastSonsAge--;
                    }

                    // Here!
                    if (yearsAgo >= 0) {
                    System.out.println("The father was last twice as old as the son " + yearsAgo + " years ago.");
                    }

                    for (yearsFromNow = 0; futureFathersAge != 2 * futureSonsAge; yearsFromNow++) {
                    futureFathersAge++;
                    futureSonsAge++;
                    }

                    if (yearsFromNow >= 0) {
                    System.out.println("The father will be twice as old as the son in " + yearsFromNow + " years from now.");
                    }

                    }


                    You can also stop the loop when it reaches negative values to make your program faster:



                    for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge && yearsAgo >= 0; yearsAgo++) {





                    share|improve this answer













                    Didn't you notice that your program runs quite slowly? :)



                    For the (8, 3) years ago case, your for loop keeps looping and looping, trying to find a year that the father is twice as old, but as we know, the father will only become twice as old in the future, but not in the past. The for loop doesn't know this and it will try very hard to find such a year. It tries so hard that yearsAgo is incremented past the max value of int. This causes an overflow, and the value of yearsAgo will "wrap back around" to the minimum value of int, which is a negative number. And then this negative number will get incremented many many times, until -2.



                    The same goes for the other case.



                    To fix this, you can add if statements to check if the results are negative:



                    public static void twiceAsOld (int currentFathersAge, int currentSonsAge) {

                    int yearsAgo;
                    int yearsFromNow;
                    int pastFathersAge = currentFathersAge;
                    int pastSonsAge = currentSonsAge;
                    int futureFathersAge = currentFathersAge;
                    int futureSonsAge = currentSonsAge;


                    for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge; yearsAgo++) {

                    pastFathersAge--;
                    pastSonsAge--;
                    }

                    // Here!
                    if (yearsAgo >= 0) {
                    System.out.println("The father was last twice as old as the son " + yearsAgo + " years ago.");
                    }

                    for (yearsFromNow = 0; futureFathersAge != 2 * futureSonsAge; yearsFromNow++) {
                    futureFathersAge++;
                    futureSonsAge++;
                    }

                    if (yearsFromNow >= 0) {
                    System.out.println("The father will be twice as old as the son in " + yearsFromNow + " years from now.");
                    }

                    }


                    You can also stop the loop when it reaches negative values to make your program faster:



                    for (yearsAgo = 0; pastFathersAge != 2 * pastSonsAge && yearsAgo >= 0; yearsAgo++) {






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 3 at 2:06









                    SweeperSweeper

                    70.5k1075143




                    70.5k1075143























                        1














                        When I debug your code I can see that yearsAgo is incrementing without bound, causing pastFathersAge and pastSonsAge to go into negatives. This is causing negative integer overflow. This happens because your condition pastFathersAge != 2 * pastSonsAge is never met (rather, never NOT met). Not until your futureFathersAge has gone all the way through the negatives, back into positives, and finally settles on -2.



                        The moral of the story is to make certain that your terminating condition for your loop can always can be met. Don't use !=, use >= or <= instead.






                        share|improve this answer






























                          1














                          When I debug your code I can see that yearsAgo is incrementing without bound, causing pastFathersAge and pastSonsAge to go into negatives. This is causing negative integer overflow. This happens because your condition pastFathersAge != 2 * pastSonsAge is never met (rather, never NOT met). Not until your futureFathersAge has gone all the way through the negatives, back into positives, and finally settles on -2.



                          The moral of the story is to make certain that your terminating condition for your loop can always can be met. Don't use !=, use >= or <= instead.






                          share|improve this answer




























                            1












                            1








                            1







                            When I debug your code I can see that yearsAgo is incrementing without bound, causing pastFathersAge and pastSonsAge to go into negatives. This is causing negative integer overflow. This happens because your condition pastFathersAge != 2 * pastSonsAge is never met (rather, never NOT met). Not until your futureFathersAge has gone all the way through the negatives, back into positives, and finally settles on -2.



                            The moral of the story is to make certain that your terminating condition for your loop can always can be met. Don't use !=, use >= or <= instead.






                            share|improve this answer















                            When I debug your code I can see that yearsAgo is incrementing without bound, causing pastFathersAge and pastSonsAge to go into negatives. This is causing negative integer overflow. This happens because your condition pastFathersAge != 2 * pastSonsAge is never met (rather, never NOT met). Not until your futureFathersAge has gone all the way through the negatives, back into positives, and finally settles on -2.



                            The moral of the story is to make certain that your terminating condition for your loop can always can be met. Don't use !=, use >= or <= instead.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 3 at 2:18

























                            answered Jan 3 at 2:05









                            Stalemate Of TuningStalemate Of Tuning

                            542315




                            542315






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015399%2fwhat-would-cause-a-for-loop-to-decrement-when-its-supposed-to-increment%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas