# Endless Loop will not stop and does not increment correctly

• (3 Pages)
• 1
• 2
• 3

## 39 Replies - 5955 Views - Last Post: 20 October 2012 - 09:56 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295344&amp;s=730698f53ac8c0804126d2e500615247&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 jon.kiparsky

• Pancakes!

Reputation: 8930
• Posts: 15,434
• Joined: 19-March 11

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 10:02 AM

ValeriePurvis, on 13 October 2012 - 11:53 AM, said:

You just ruined my day! Math != FUN Well I suppose I have a fair amount of fun with accounting but others might find that exceptionally boring! I will have to try to look at math a little differently then.... perhaps.

Have a question.... been thinking..... would I want to do an if numStars.equal (-1) to trigger the exit to the loop if that statement were true?

numStars is an int, I think? If so, then it doesn't have methods - it's a primitive. (we'll talk about classes and primitives another time). In that case you'd compare it with == like so

```if (numStars == -1) {  blah blah }
```

But yes that would be a good condition to use to exit the loop.

Schematically:
```in the loop
{
get input
if the input is the exit value
exit the loop
process the input normally // if we get to here, we haven't exited the loop
}
```

The other approach is very, very similar. Maybe you can try to sketch it out in the same way. (you can cut and paste - there will be very few changes, and they will be mostly to the comment)

Math is fun. You've just been doing arithmetic, and there's a lot more to math than arithmetic.

For example try to answer this one: is the set of positive integers larger than the set of positive even integers?

### #17 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 12:42 PM

No it's a double Here's the code changed to be what it is now not what it was.... Sometimes progress is painful....

```{
numStarsString = JOptionPane.showInputDialog ("Enter Number of Stars between 1 and 4: ");		// Get input.

// This is the work done in the detailLoop() method

numStars = Double.parseDouble (numStarsString);	// Convert to double.
while (numStars >= 1 && numStars <=4)			// Write while loop here
{

numStars = Double.parseDouble (numStarsString);	// Convert to double.
totalStars += numStars;
System.out.println ("Number of Patrons Responding: " + numPatrons);
numPatrons ++;
System.out.println ("Number of Stars Given: " + numStars);
numStarsString = JOptionPane.showInputDialog ("Enter Number of Stars between 1 and 4: ");		// Get input.
}
if (numStars == -1)
{
averageStars = (totalStars+1)/numPatrons;					// Calculate average star rating
System.out.println("Average Star Value: " + averageStars);
}

// This is the work done in the endOfJob() method

System.out.println ("End of File");
}
System.exit(0);

```

The Answer to the Integer question is NO.... Not only are numbers infinite, but there are an equal number of odd and even 0,2,4,6,8 and 1,3,5,7,9

### #18 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,143
• Joined: 27-December 08

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 12:48 PM

So what specifically is wrong with your code? Any errors? How is it performing not as it should? Or were you able to finish it?

Quote

The Answer to the Integer question is NO.... Not only are numbers infinite, but there are an equal number of odd and even 0,2,4,6,8 and 1,3,5,7,9

Actually the set of integers can be partitioned into {0, {Even Integers}, {Odd Integers}}. We know that the cardinality of the set of Even Integers has to be less than that of the set of Integers, by definition of a subset. The only way they could be equal is if the set of integers contains only even integers, which is clearly not the case. Therefore, the cardinality of the set of even integers has to be less than that of the set of integers.

### #19 jon.kiparsky

• Pancakes!

Reputation: 8930
• Posts: 15,434
• Joined: 19-March 11

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 12:58 PM

macosxnerd101, on 13 October 2012 - 02:48 PM, said:

Quote

The Answer to the Integer question is NO.... Not only are numbers infinite, but there are an equal number of odd and even 0,2,4,6,8 and 1,3,5,7,9

Actually the set of integers can be partitioned into {0, {Even Integers}, {Odd Integers}}. We know that the cardinality of the set of Even Integers has to be less than that of the set of Integers, by definition of a subset. The only way they could be equal is if the set of integers contains only even integers, which is clearly not the case. Therefore, the cardinality of the set of even integers has to be less than that of the set of integers.

Doh! Mac, you disappoint me. Go back and hava a look at Cantor!

For every integer, there is one corresponding even integer, namely n -> 2n, and every even integer corresponds to exactly one integer ( n -> n/2)
Therefore, the set of integers and the set of even integers are equaly in their extent: each element of one set maps onto exactly one element in the other set.

Remember, we're talking about infinite sets here.

### #20 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,143
• Joined: 27-December 08

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 01:03 PM

This isn't an onto function though, so their cardinalities cannot be the same size. And just because we can't count them doesn't mean we can't prove that their cardinalities are not the same. Cantor's proof was interesting by the way. Anyways, we digress a little from the OP's question.

This post has been edited by macosxnerd101: 13 October 2012 - 01:08 PM
Reason for edit:: Part of that was nonsense.

### #21 jon.kiparsky

• Pancakes!

Reputation: 8930
• Posts: 15,434
• Joined: 19-March 11

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 01:08 PM

Hm. We are straying a fair bit - the question was raised primarily to offer an example of math that is
a) not arithmetic and
fun

But it is a total tangent (and I still think Cantor's proof is sound)

ValeriePurvis, is that code working for you? It's hard to validate code by reading it, although if you point out what errors you're seeing it's pretty easy to spot where they would be happening.

### #22 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 01:27 PM

NOPE.. tried to do an .equal but it said it could not "dereference" a double. Other than that when I use ==-1 it parses just fine with the following exceptions:

#1. When I enter -1 it goes through the loop one more time and I have to enter the -1 a second time this makes my average 1.66666666666..... instead of 2.5 this happens when I enter any number, even though I want it to stop or go back and say that I've entered an invalid number for anything >4.(greater than)

I think, in my totally confused state, that I need to move my if statement...to inside the loop....currently it is outside the while group of statements. Lines 17 through 20.
I've not done that yet... I was having too much fun watching the conversation.... thank you for lightening the day.

I know it's probably just "logic" but at the moment I'm not getting it.....

### #23 jon.kiparsky

• Pancakes!

Reputation: 8930
• Posts: 15,434
• Joined: 19-March 11

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 03:54 PM

double is also a primitive - have to use == comparison, just as with an int.

### #24 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 06:56 PM

That's a truth I figured out while reading some links that Greg had given me a while back. Thanks to both of you I have learned and clarified a great deal this past couple of days. I appreciate your honesty and intelligence and I hope that you will continue to help me learn.

That being said. What exactly, not textbookly, is the purpose of the do / while loop? Does it really simplify the process or merely give you a different way to arrange stuff? Gregg you are invited to respond as well...

### #25 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,143
• Joined: 27-December 08

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 07:09 PM

The difference is the order of execution. A do-loop will execute at least once, while a while loop will not necessarily execute.

Given a while loop, the condition is evaluated first. If true, then the loop will execute, then the condition will be evaluated again. This will be repeated until the condition becomes false or there is a break statement in the loop.
```while(condition){
//code
}

```

Given a do-loop, the condition is evaluated after the loop code executes. It is otherwise, very similar to a while loop.
```do{

}while(condition);

```

In fact, you can use either loop to achieve the same result with clever use of boolean conditions. Sometimes it's just easier to use one over the other though.

For example, if you have a game loop for a guessing game, it might be easier to use a do-loop with the condition that the user wants to play again. Then after the user plays one round, ask them if they want to play again. This way, the condition at the top of the while loop doesn't prevent the loop from executing.
```boolean playAgain = false;

do{

}while(playAgain);

```

I could achieve the same result with a while loop, however, if I set playAgain = true initially.
```boolean playAgain = true;

while(playAgain){

}

```

### #26 jon.kiparsky

• Pancakes!

Reputation: 8930
• Posts: 15,434
• Joined: 19-March 11

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 07:17 PM

Yes, what he said.

### #27 YasuoDancez

Reputation: 20
• Posts: 135
• Joined: 30-September 09

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 08:02 PM

I have some very simple advise. What I tend to do when I have problems with some code ( as most here probably do similiar ) is put system.out.println() statements throughout my code where I'm having problems. Use this to print out the state of your variables. This will help a lot to see what is occurring. The next step after this, if the printouts do not work, would be to go straight for a debugger.

In your situation, print out the state of your numStars before and after increments, and any other variables that are important.

On another tune, Math is not as bad as the name it has. I kinda dislike hearing people say that now. Math use to scare me a little bit, but I started to realize ( through my professor's emphasis on math ) how important it is or it can be to programming. Between that and not wanting to believe the hype that math is hard, and not wanting to fall into the general stereotypical category of disliking it, I forced my self to pay closer attention to it and embrace it.

Seriously though, I think that any person that is interested in programming should get to a comfortable level with mathematics. It will help you out greatly with coding projects. There are also a few things in Discrete Mathematics that are perfect for programmers.

### #28 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 08:12 PM

Ok! Little lightbulbs are turning on... essentially then,

If I want to ensure that there is at least one entry to compare it might be easier/better to use the do/while.

So I'm still trying to figure out what is making the -1 mess up my figures. I tried putting the if statement inside the while loop, that didn't seem to work, So I moved it back outside of the loop.... Instead of using <= or >= I created a couple of additional declarations final double MIN_STARS = 1 and final double MAX_STARS = 4 That helped it quit if something more than 4 was entered but it didn't keep the -1 input from being recognized. but my while segment reads such:
```
while (numStars >= 1  && numStars <= 4)
{
numStars = Double.parseDouble (numStarsString);// Convert to double.

totalStars = (totalStars + numStars);
System.out.println ("Number of Stars Given: " + numStars);

numPatrons ++;
System.out.println ("Number of Patrons Responding: " + numPatrons);

(" Enter Number of Stars between 1 and 4: ");
}

```

then outside the while loop I added an if statement which says
```if (numStars == -1) // This is the admin entry to stop the loop and calculate average.
{
averageStars = totalStars/numPatrons);	// Calculate average star rating
System.out.println("Average Star Value: " + averageStars)
}

```

I feel this is where my problem lies. But I don't know what exactly I'm doing wrong. (Am I getting better at explaining myself? )
Is there a way to make that final entry of -1 not be included in the calculations and messing up the average?

### #29 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 08:34 PM

Ok, so let's step through the lines of code you have in your first segment, and let's assume that numStars was set to 3 and numStarsString set to "-1" for argument sake.

``` while (numStars >= 1  && numStars <= 4)
// This is true, so we enter the while loop
{
numStars = Double.parseDouble (numStarsString);// Convert to double.
// numStars is now equal to -1

totalStars = (totalStars + numStars);
// We just added the -1 to totalStars

```

As you can see from the comments above, you are actually entering the while loop after the user enters -1.

The reason you are entering the loop is that the variable you are ACTUALLY using to decide whether to run the next iteration or not is not set until AFTER you make the decision.

In the code you included above, you ensure numStars is within the valid range and after that you set it to the input from the user.

The way to fix this problem is to set numStars right after you capture input (at the end of the while loop, right after you set numStarsString.

Hope this helps.

### #30 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 08:48 PM

It helps a little. I"m sort of unclear as to what you mean though. Let me see if I have this down...

So I need to put the double parse double part before the while loop?

( I have that exact statement before the loop starts. when I compiled and run the program it did not work until I entered it where it is now. )

hmm.. perhaps you lost me... or it's too late... I'll look at it again in the morning. Night All! And thanks!