small question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

small question

 
0
  #1
May 4th, 2007
Could you tell me the mean of this code?
  1. while(1)
  2. { // do something
thanks!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 488
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: small question

 
0
  #2
May 4th, 2007
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,
  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.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: yesm is an unknown quantity at this point 
Solved Threads: 2
yesm yesm is offline Offline
Newbie Poster

Re: small question

 
0
  #3
May 4th, 2007
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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: small question

 
1
  #4
May 5th, 2007
Originally Posted by yesm View Post
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:
  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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

Re: small question

 
0
  #5
May 5th, 2007
thank you for your helps
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC