# Question: how to kill loop, soon as invalid test score

Page 1 of 1

## 7 Replies - 613 Views - Last Post: 10 May 2012 - 10:53 PMRate 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=278845&amp;s=43a8ec47b869b59934f1a001ed8219ab&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 animus

Reputation: 0
• Posts: 11
• Joined: 10-May 12

# Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 04:54 PM

My question is how do I kill the loop as soon as the test score becomes outside its limits (lets say 1-3 are right but 4th time through isn't)?

```
//calculates test average and test score total.
#include <iostream>
using namespace std;
int main ()
{
int limit;
double score = 0;
double total = 0;
double entered = 0;
double average;
bool precede = true;
char response;
cout << "How many tests: " << endl;
cin >> limit;
for(int count = 0;count < limit; count++)
{

score = score;
cout << "Enter score   #" << count +1 << endl;
cin >> entered;
if (entered <= 0 || entered >= 100)
{
cout << "Invalid test score" << endl;
precede = false;
}

{
score = score + entered;
}
}

total = total + score;
cout << "Total " << total << endl;
average = total/limit;
cout << "Average " << average << endl;
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Question: how to kill loop, soon as invalid test score

### #2 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 05:00 PM

You can always use the break keyword to well, break out of loops or continue to reset a loop at its next iteration.

### #3 snoopy11

• Engineering ● Software

Reputation: 1377
• Posts: 4,318
• Joined: 20-March 10

## Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 05:02 PM

Hi,

Use a
break;
Statement

http://msdn.microsof...w(v=vs.80).aspx

Best Wishes

Snoopy.

Ahh ninja'd by DogStopper !
I knew I shouldn't have looked for that link

This post has been edited by snoopy11: 10 May 2012 - 05:03 PM

### #4 animus

Reputation: 0
• Posts: 11
• Joined: 10-May 12

## Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 05:27 PM

Dogstopper, on 10 May 2012 - 05:00 PM, said:

You can always use the break keyword to well, break out of loops or continue to reset a loop at its next iteration.

Is there another way to do it without break/continue, such as using a while, or an if, if else?

### #5 snoopy11

• Engineering ● Software

Reputation: 1377
• Posts: 4,318
• Joined: 20-March 10

## Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 05:34 PM

Why what's wrong with break,

You could just wrap everything
In a while loop

while (precede)

{
// do all your for loop stuff here.
}

### #6 turboscrew

• D.I.C Lover

Reputation: 152
• Posts: 1,032
• Joined: 03-April 12

## Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 10:29 PM

You can have a while-loop with a flag indicating whether to loop still or stop looping, but that's considered a bad way. Break is better.

### #7 snoopy11

• Engineering ● Software

Reputation: 1377
• Posts: 4,318
• Joined: 20-March 10

## Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 10:35 PM

turboscrew, on 11 May 2012 - 05:29 AM, said:

You can have a while-loop with a flag indicating whether to loop still or stop looping, but that's considered a bad way. Break is better.

Yes I agree
But apparently he doesn't want to use break;

I have no idea why ....

Snoopy.

### #8 turboscrew

• D.I.C Lover

Reputation: 152
• Posts: 1,032
• Joined: 03-April 12

## Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 10:53 PM

Also:
for(int count = 0;(count < limit) || (score < limit); count++)
can be used, but it's a bit ugly too as so is

for(int count = 0, flag = true;(count < limit) && flag; flag = (score <= high_limit) && (score >= low_limit), count++)