Int Main explanation please


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
}
  
Yes, your description is correct.

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++!)
Last edited on
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
@dutch

Slightly off-topic, but an immediately-invoked function has the advantage of being an expression:

#define FUNC1() ([](){ f(); g(); }())
Last edited on
@mbozzi, I never thought of that! That's cool. I wonder if it has some extra overhead? Could be optimized out, though.
Recent fashion is to use these to initialize const[expr] objects:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es28-use-lambdas-for-complex-initialization-especially-of-const-variables

I wonder if it has some extra overhead?

In this contrived test both GCC and Clang are quick to optimize:
https://godbolt.org/z/yNftPp
> #define FUNC1() ([](){ f(); g(); }())

#define for this is unnecessary, and often turns out to be a bad idea.

Consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void f() ;
void g() ;

#define FUNC1() ([](){ f(); g(); }())

namespace A {

    int f( const char* txt = "" ) ;
    struct g { int a = 0 ; };

    // FUNC1() ???
}

struct B {

    // ...

    // FUNC1() ???
};
Thanks!
Only fully-qualified names should be used in macros.
Last edited on
> In real code only fully-qualified names should be used in macros.

In real C++ code, macros should not be used if there is a reasonable alternative.

Here, const auto FUNC1 = [] { f() ; g() ; }; is the reasonable alternative.
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?
const int N = 5; or to be pedantic, constexpr int N = 5 ;

See: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-macros2


> Would the compiler optimize out of having to allocate memory to the variable
> and is this guaranteed by at least most compilers?

In practice, yes and yes.
As a bonus, in a debug build, you would be able to examine the value of the variable in a debugger.
Topic archived. No new replies allowed.