| | |
small question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Could you tell me the mean of this code?
thanks!
C++ Syntax (Toggle Plain Text)
while(1) { // do something
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, 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
eg,
CPP Syntax (Toggle Plain Text)
void somefunc() { std::string str; while(true) { std::getline(cin, str); if( str == "quit" ) return; else std::cout << "You Typed: " << str << std::endl; } }
Last edited by Bench; May 4th, 2007 at 11:31 am.
¿umop apisdn upside down? •
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 2
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:
However, some form of exit condition works better, like this:
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!
C++ Syntax (Toggle Plain Text)
int main() { int bob; while(1) { cout << "Enter a number to display, or -1 to exit: "; cin >> bob; if(bob == -1) { return 0; } cout << bob << endl; } }
However, some form of exit condition works better, like this:
C++ Syntax (Toggle Plain Text)
int main() { int bob; char exit = false; while(!exit) { cout << "Enter a number to display, or -1 to exit: "; if(bob == -1) { exit = true; } else { cout << bob << endl; } } }
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.
•
•
•
•
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)
int main() { int bob; while(1) { cout << "Enter a number to display, or -1 to exit: "; cin >> bob; if(bob == -1) { return 0; } cout << bob << endl; } }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- hijackthis log... what to delete?? (Viruses, Spyware and other Nasties)
- quick question (MS SQL)
- small question please (PHP)
- a short question in Java (Java)
- Javascript implementation question (JavaScript / DHTML / AJAX)
- Question about structs in managed C++ (C++)
- reference problems (C)
- program design (Visual Basic 4 / 5 / 6)
- Upgrading to Win2000 from Win98 advice wanted! (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: what on earth is MFC
- Next Thread: .rc file??
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple spoonfeeding string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






