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 2 of 2

Thread: while loop

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default while loop

    I'm trying to get my while loop to include negative numbers except -9999.
    I could get my code to read positive numbers like 5 having a sum of odd numbers of 9, but trying to get negative numbers never worked such as this:
    System.out.println("Enter a number: ");
    		int num = input.nextInt();
     
    		int sum = 0;
     
    		while (num > 0) {
    			sum += num;
    			num = num - 2;
    		}
    		System.out.println(sum);

    This code works for positive numbers, but not negative. I couldn't get it to work for negative numbers, as it would give me like -2362970237907 or something similar.

    I've tried several different ways including getting an output of really high negative numbers when entering -5.

    This is the best I could do with the code although it doesn't give me an output for some reason when i enter a number like 5. Why isn't it?

    Here's my code:

    System.out.println("Enter a number: ");
    			int num = input.nextInt();
     
    			int sum = 0;
     
    				while (num >= 0 || num < 0) {
    					if (num >= 0) {
    						sum += num;
    						num = num - 2;	
    					}
    					else {
    						sum += num;
    						num = num + 2;
    					}
     
    				}
    			System.out.println(sum);


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: while loop

    while (num >= 0 || num < 0) {
    When do you want the loop to stop looping? There are no values of num that will cause the loop to end.

    it doesn't give me an output
    Does the program ever exit or does it run forever?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  5. [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