Results 1 to 4 of 4
  1. #1
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default eternal loop problem

    why is this loop not stopping?
    Java Code:
     public static final int END = Integer.MAX_VALUE;
        public static final int START = END - 100;   
        public static void main(String[] args) {
          int count = 0;
          for (int i = START; i <= END; i++)
            count++;
          System.out.println(count);
        }

  2. #2
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    All int variables are always less tha or equal to Integer.MAX_VALUE, which is defined as the highest int value. When i gets to Integer.MAX_VALUE andi s incremented, it silently wraps around to Integer.MIN_VALUE, thus the eternal loop.
    p

  3. #3
    levent Guest

    Default

    All int variables are always less tha or equal to Integer.MAX_VALUE, which is defined as the highest int value. When i gets to Integer.MAX_VALUE andi s incremented, it silently wraps around to Integer.MIN_VALUE, thus the eternal loop.

    Hmm.. You are right. Congratulation pegitha, you are careful.

  4. #4
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default

    Thanks for that info. There is an awful lot I need to learn.
    sandor

Similar Threads

  1. Loop Help
    By HeavyD in forum New To Java
    Replies: 7
    Last Post: 09-22-2010, 09:55 PM
  2. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 06:44 AM
  3. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 03:51 PM
  4. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •