Is it possible to execute a function once while the function is executed within a while-loop?
example:
Code:// includes and stuff void greet() { cout << "Greetings!"; } int main() { while (bla == "bla") { greet(); // execute once? } }
Is it possible to execute a function once while the function is executed within a while-loop?
example:
Code:// includes and stuff void greet() { cout << "Greetings!"; } int main() { while (bla == "bla") { greet(); // execute once? } }
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
The easiest would be moving the call outside the loop.
But since you ask the question, there must be a reason why you can't or don't want to do it.
What is that reason?
Similarly, if it had to be in the loop, you could simply put a count on the loop and put your function in a conditional statement that only executes on the first count.
Code:int loopCount = 0; while (blah == "Blah") { loopCount++; if (loopCount == 1) greet(); }
Sent from my iPadŽ
Code:// includes and stuff void greet() { cout << "Greetings!"; } int main() { bool hasbeenused = false; while (bla == "bla") { if (!hasbeenused) { greet(); // execute once? hasbeenused = true; } } }
thx for the replys!
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc