You are wondering, why we are not limited to writing either
if ( true )
or
if ( false )
. That would be rather limiting, wouldn't it?
What language syntax has is:
[output]IF ( expression )[/code]
The expression is
evaluated during runtime and the result of evaluation is either true or false. That makes sense, doesn't it?
Now, lets take a simple example:
if ( a < b )
That isn't a function call.
... or is it?
Lets reveal a bit more:
1 2 3 4 5 6
|
bool operator< ( const Foo &, const Foo & ); // function declaration
// code
Foo a = ...;
Foo b = ...;
if ( a < b )
|
What did look like an innocent "is a less than b" expression, is suddenly a function. Just to be sure, lets write that last line again, but avoiding the operator syntax:
if ( operator< ( a, b ) )
The condition expression contains a function call. The expression can be evaluated only by calling the function.
Does that answer your question?
Then the lazy fellow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
using std::cout;
bool foo() {
cout << "Hello ";
return false;
}
bool bar() {
cout << "world ";
return true;
}
int main() {
if( foo() && bar() ) {
cout << "Dolly!\n";
}
cout << '\n';
return 0;
}
|