Trying to understand C++ if syntax

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 :)



 
closed account (N36fSL3A)
If statements just execute if he value is true. You could actually just write true in there.
http://www.cplusplus.com/doc/tutorial/program_structure/

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.

Here is an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

bool isNegative( int number )
{
    if( number < 0 )  //"<" will return true if that is correct, false if not.
        return true;    //if false was returned above this code runs
    else  
        return false;  //else 
}

int main()
{
    if( isNegative( -5 ) )  //if accepts a boolean for argument, isNegative returns boolean
        cout << "Is negative" << endl;
    else 
        cout << "Is positive" << endl;
    return 0;
}
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

boolean myVariable = InitInstance (hInstance, nCmdShow);

if (myVariable = true) then

stuff.

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.
closed account (N36fSL3A)
Make sure you put == instead of =.
conditional control structures such as if or the ternary operator implicitly compare the truth-ness of an expression by default.

Anything you provide these control structures with as conditions are actually expressions.

in your example, if not the value returned by InitInstance() is a non-zero value is true, execute the control structure body.
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)

(First of all, you're missing a second equals sign there.)

Doesn't the same thing work in Java as well?
http://ideone.com/sXDxFB
Yes I am (missing another equals sign that is) :) And if it does work, I've never seen or used it that way myself but this stuff is good to know :)
Topic archived. No new replies allowed.