Thread: Looping

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    2

    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. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The modulus operator is your friend.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, that would skip every floor divisible by 13, like 26.
    Code:
    for(i = 1; i <= floors; ++i) if(i != 13) {
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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*
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You know, the funny thing is that it probably results in the same machine code as my version.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    *crawls out from under blanket*
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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;?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Registered User Paz_Rax's Avatar
    Join Date
    Jan 2004
    Posts
    16
    If break is bad coding then are switch statments also?
    "Life, it's all in how you script it."

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, what leads you to this conclusion?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by CornedBee
    No, what leads you to this conclusion?
    From the confusing way this thread reads.

    Recap:
    break and continue are very useful statements when used as they should be.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    Registered User Paz_Rax's Avatar
    Join Date
    Jan 2004
    Posts
    16
    No, what leads you to this conclusion?
    You must use break in switch statments, That is why I ask. It didn't seem right to say it was but WaltP got it.
    "Life, it's all in how you script it."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM