Results 1 to 4 of 4
  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default While loop/if statement problem

    Sorry for my awful title. I can't even explain my problem I'm having without is being to long.

    I was working on a project today and the while loop/if statement is acting really weird.

    Here is my SSCCE code (I know "while(true)" don't comment on it):
    Java Code:
    public class WhileLoop {
    	public WhileLoop(){
    		int i = 0;
    		while(true){
    			i++;
    			if(i==-1){
    				System.out.println("Test");
    			}
    		}
    	}
    	public static void main(String args[]){
    		new WhileLoop();
    	}
    }
    when I run this it keeps printing Test all the time. So my question is how can the variable be equal to -1 when I add one to it all the time?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: While loop/if statement problem

    Hi , you have an overflow. When i reaches Integer.MAX_VALUE and you add 1 you end up at Integer.MIN_VALUE. Then your loop adds until you reach -1 again.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: While loop/if statement problem

    An int has 32 bits; its valid range is from Integer.MIN_VALUE to Integer.MAX_VALUE. If it has reached its maximum value and you add one to it, it has its minimum value. Somewhere in the middle is the value -1, so after 2^32 (a bit more tha four billion) cycles (loop iterations), the value is -1 again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: While loop/if statement problem

    okay, thanks for the quick response guys, I appreciate it.

Similar Threads

  1. How to add loop statement
    By Hawks in forum New To Java
    Replies: 4
    Last Post: 02-24-2013, 10:24 PM
  2. problem with if statement in while loop of result set
    By kishan.java in forum New To Java
    Replies: 6
    Last Post: 04-09-2011, 04:07 PM
  3. Help with loop statement
    By arvind1508 in forum New To Java
    Replies: 2
    Last Post: 02-23-2011, 04:39 PM
  4. Need help with a loop statement
    By sunshine39 in forum New To Java
    Replies: 7
    Last Post: 11-03-2008, 03:42 AM
  5. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 01:41 AM

Posting Permissions

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