So my question is I am running a
main function of my problem is a bool which is asking the player to play again.
When the function is initialized it has the value of false meaning it is a 0 or a no in booling terms so why does it even loop if it is false to begin with ?
once it begins to loop it calls out to my function
print intro and play game
once it runs thru those
it calls out to ask to play again function and takes its answer and applies it to the bool b play again and if they player says no it exits if he says yes or 1 then it loops back all over again.
this is my understanding of this function main am I getting this correctly or can someone elaborate better for me Thank you
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
bool bPlayAgain = false ;
do {
PrintIntro();
PlayGame();
// Add game summary
bPlayAgain = AskToPlayAgain();
}
while (bPlayAgain);
return 0;// exit application
}
In a "do/while" loop, the condition isn't tested until after the body is executed, unlike a "while" loop, where the condition is tested first. So a "do/while" loop body will always execute at least once. In fact, there is a common idiom for macros that leverages this fact to properly contain the code no matter the surrounding context:
#define FUNC() do { f(); g(); } while(0)
The contained code will run exactly once, as if the surrounding do/while wasn't even there.
(Although you should of course avoid macros in C++!)
thank you sir appreciate the explanation behind that i am also having trouble with another loop ill post it in a separate post if you can take a look at that one as well and break it down apprecaite tons thanks alot guys
Would you use const int = 5; over a macro even though the variable is assigned compile-time and is never re-assigned? But why - Would the compiler optimize out of having to allocate memory to the variable and is this guaranteed by at least most compilers?