944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1023
  • C++ RSS
May 4th, 2007
0

small question

Expand Post »
Could you tell me the mean of this code?
C++ Syntax (Toggle Plain Text)
  1. while(1)
  2. { // do something
thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006
May 4th, 2007
0

Re: small question

Its a way of starting an infinite loop which will be stopped using some means other than the condition inside the brackets next to while.

eg,
CPP Syntax (Toggle Plain Text)
  1. void somefunc()
  2. {
  3. std::string str;
  4. while(true)
  5. {
  6. std::getline(cin, str);
  7. if( str == "quit" )
  8. return;
  9. else
  10. std::cout << "You Typed: " << str << std::endl;
  11. }
  12. }
IMHO, this kind of idiom is seldom useful - unless you have some compelling reason to do otherwise, you should put an exit condition in the while brackets
Last edited by Bench; May 4th, 2007 at 11:31 am.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
May 4th, 2007
0

Re: small question

Personally, I sometimes use while(1) or similar for things like menus, where choosing a menu option would cause the program to do something and then return to the menu. Then, if you want to exit the program, just return 0 to end the program:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int bob;
  4. while(1)
  5. {
  6. cout << "Enter a number to display, or -1 to exit: ";
  7. cin >> bob;
  8. if(bob == -1) { return 0; }
  9. cout << bob << endl;
  10. }
  11. }

However, some form of exit condition works better, like this:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int bob;
  4. char exit = false;
  5. while(!exit)
  6. {
  7. cout << "Enter a number to display, or -1 to exit: ";
  8. if(bob == -1) { exit = true; }
  9. else { cout << bob << endl; }
  10. }
  11. }

Of course, your program would probably be a little more complicated.

Edit: Ha, totally didn't see that it had already been answered. My bad!
Last edited by yesm; May 4th, 2007 at 12:24 pm. Reason: Giving credit to previous poster.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
yesm is offline Offline
15 posts
since May 2007
May 5th, 2007
1

Re: small question

Click to Expand / Collapse  Quote originally posted by yesm ...
Personally, I sometimes use while(1) or similar for things like menus, where choosing a menu option would cause the program to do something and then return to the menu. Then, if you want to exit the program, just return 0 to end the program:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int bob;
  4. while(1)
  5. {
  6. cout << "Enter a number to display, or -1 to exit: ";
  7. cin >> bob;
  8. if(bob == -1) { return 0; }
  9. cout << bob << endl;
  10. }
  11. }
This is generally not recommended, although it does work. The reason is you are burying the exit from the function/program in the middle of your code. This could be a maintenance nightmare for the team that inherits your program (or for you in 2 months ) It would be best to break out of the loop and return at the bottom of the function.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
May 5th, 2007
0

Re: small question

thank you for your helps
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: what on earth is MFC
Next Thread in C++ Forum Timeline: .rc file??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC