Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Confused about infinite loop

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    My Mood
    Bored
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Confused about infinite loop

    Let's take an example

    int a = 1;
    for(;a>0;a++)
    In the code above the condition is always true so the loop is infinite but int datatype have a limit so what will happen if value of "a" crosses it.Is it really infinite ?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Confused about infinite loop

    When the range is exceeded, the int will turn negative and thus breaking out of the loop.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Confused about infinite loop

    The correct way to do the loop would be:

    		int a = 1;
    		for(int b = 0; b < a; b++){
    			System.out.println("JavaProgrammingForums.com");
    		}

    The loop will loop by the value set in a.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Confused about infinite loop

    What newbie said. You would get what is known as an integer overflow, if I remember the term correctly.

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  2. Confused about my Career
    By DanBrown in forum The Cafe
    Replies: 10
    Last Post: February 9th, 2011, 07:47 AM
  3. confused on loop
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2010, 03:26 PM
  4. Doubling hashing, infinite loop?
    By vluong in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 8th, 2009, 01:26 AM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM