I've done some courses in Visual Basic and a lot more in Java. Now I've just begun learning C++, except this time I'm teaching myself. I've run in a snag with the very basic if statements of C++. I've always used if statements with clear logic: if x is something, do something. However, in C++ I keep running in to if statements that don't necessarily contain operators at all, or have function calls in them which also is a new thing for me. For instance:
1 2 3 4 5
if(SomeBoolReturningFunc())
{
//do some stuff
//do some more stuff
}
I don't understand the logic how that is an if statement. I see a function call, but I don't see the terms of the If itself.
Then:
1 2 3 4 5
bool AwesomeResult = SomeBoolReturningFunc();
if(AwesomeResult)
{
//do some other, more important stuff
}
I don't understand how a single variable can be considered an if statement. In java I would have done something like if (AwesomeResult = true) then stuff, to me that makes sense. Can anyone help me out and paraphrase this for me, thank you very much :)
An if statement takes a bool as an argument, so when you type something like if( x > y ), what really happens is the greater-then operator ( > ) returns ether a true or false. If its true then the if statement goes into the if body, if its false it continues.
Thank you very much for clearing that up. I see it's essentially the very same thing as what I would've done in java, except shorter. Also thanks for the very detailed example :)
So if I'm understanding right, what's happening in this line of code taken from a book I'm reading, is that a function named InitInstance is being called from within an if statement with two parameters. The function returns either 0 (false) or 1 (true). As it is written with an (!) operator, if the function returns something that does not equal true, then return 0 and end the program.
If (!InitInstance (hInstance, nCmdShow)) return 0;
Very basic stuff apparently, I just got confused as I would have written the same thing else wise more something like this
Didn't realize you could do a function call inside an if statement and at the same time see if it's true or not without equaling it against something with an operator.