1. ## 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;
}```

2. The modulus operator is your friend.

3. No, that would skip every floor divisible by 13, like 26.
Code:
```for(i = 1; i <= floors; ++i) if(i != 13) {
}```

4. 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.

5. 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*

6. You know, the funny thing is that it probably results in the same machine code as my version.

7. *crawls out from under blanket*

8. 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;?

9. 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.

10. 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 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.

11. Originally posted by Hunter2
I don't think you got the point. I said "evil", not evil 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!

12. If break is bad coding then are switch statments also?

13. No, what leads you to this conclusion?

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