# Looping

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-21-2004
hmr_smpsn_57
Looping
I need my loop to iterate once for each floor, skipping the entire thirteenth iteration, but I'm not sure how. Any help woul dbe very much appreciated.

Code:

```#include <iostream> #include <iomanip> using namespace std; int main() {         int floors;         int total = 0;         int number;         float mean;         cout << "How many floors does the hotel have? " << endl;         cin >> floors;         if (floors >= 1)         {                 for (number = 1; number <= floors; number ++)                 {                         total = total + number;                 }                 mean = float(total)/floors;                 cout << "The percentage of rooms occupied is " << mean << endl;         }         else if (floors == 13 || floors < 1)                 cout << "Invalid input" << endl;         return 0; }```
• 02-21-2004
joshdick
The modulus operator is your friend.
• 02-21-2004
CornedBee
No, that would skip every floor divisible by 13, like 26.
Code:

```for(i = 1; i <= floors; ++i) if(i != 13) { }```
• 02-21-2004
CornedBee
It would be more efficient but more code if you had two loops, one for the floors below 13 and one for the uppers. This way you save the comparison.

It really depends on how tight your loop is. If it's a very small block of code inside, you'd want the two loops with duplicated code. If the block is large, you could either have one loop the way I posted it, or two loops, calling a function inside. The question is whether the function call or the comparison to 13 is the larger overhead. Depends on the function of course, and the only real answer is profiling.
• 02-21-2004
Hunter2
You could also use the "evil" continue statement...
Code:

```for(int i = 1; i <= floors; ++i) {     if(i == 13)         continue;     //Do everything else you wanted to do }```
*hides*
• 02-22-2004
CornedBee
You know, the funny thing is that it probably results in the same machine code as my version.
• 02-22-2004
Hunter2
*crawls out from under blanket*
:D
• 02-22-2004
WaltP
Quote:

Originally posted by Hunter2
[B]You could also use the "evil" continue statement...
OK, I'll bite. Why is continue; evil? And if that's evil, why isn't break;?
• 02-23-2004
CornedBee
Some people consider both continue and break bad practice. I try to avoid them when they're not necessary (like here), but I don't hesitate to use them when they make sense either.
• 02-23-2004
Hunter2
Quote:

OK, I'll bite. Why is continue; evil? And if that's evil, why isn't break;?
I don't think you got the point. I said "evil", not evil :rolleyes: Personally, I think continue and break are divine, sent from the heavens to bless our existence with readable-er code. Other people disagree, as CornedBee has explained. ;)
• 02-23-2004
WaltP
Quote:

Originally posted by Hunter2
I don't think you got the point. I said "evil", not evil :rolleyes: Personally, I think continue and break are divine, sent from the heavens to bless our existence with readable-er code. Other people disagree, as CornedBee has explained. ;)
You're right, I could'n tell that "evil" meant "blessed". I thought you meant "generally frowned upon".

Oh, and "readable-er code" -- I like this! :)
• 02-24-2004
Paz_Rax
If break is bad coding then are switch statments also?
• 02-24-2004
CornedBee
No, what leads you to this conclusion?
• 02-24-2004
WaltP
Quote:

Originally posted by CornedBee
No, what leads you to this conclusion?