C++ syntax when initializing the instance variables - c++

What does it mean in C++
int x;
x = GetMethod("OpponentCalledOnTurn") == 1;
Note : why is there a "==1" part.
I am Novice to c++.

This will set x to 1 if GetMethod("OpponentCalledOnTurn") == 1 evaluates to true and to 0 if it evaluates to false.
The underlying rule here: A boolean value can be converted to other integer types, that will result in 1 for true and 0 for false.

== is the equality comparison operator.
So GetMethod("OpponentCalledOnTurn") == 1 first calls the function GetMethod, passing the given string literal as an argument. The return value of that function call is then compared to 1. That comparison evaluates to either true if the return value equals 1, or false otherwise.
x = then assigns that true or false to x. Since x is of type int and not bool (the type of true and false), true is converted to 1 and false is converted to 0.
Effectively, if GetMethod("OpponentCalledOnTurn") returns 1, x will end up equaling 1, otherwise it will end up equaling 0.

Related

Different between !a==-1 and a!=-1

Different between '(!server[i].type==-1)' and '(server[i].type!=-1)'
Server is a struct array have type and amount two members.
server[0] type:-1 amount: 100
server[1] type: 0 amount: 50
server[2] type: 1 amount: 50
I want to calculate the sum of amounts of type not -1.
I think these two codes are same
for(int i=0;i<3;i++) {
if(!server[i].type==-1)
total+=server[i].amount;
}
for(int i=0;i<3;i++) {
if(server[i].type!=-1)
total+=server[i].ptime;
}
But i found the first one not work.Can someone tell me why this happen?Thank you very much.
Well server[i].type != -1 is true if server[i].type is not equal to -1.
On the other hand, !server[i].type==-1 due to operator precedence evaluates as (!server[i].type) == -1, which is never going to be true because the left hand side is either going to be 0 or 1, unless server[i].type has operator! overloaded... which I'm assuming is not the case. The equivalent version would be !(server[i].type == -1).
The unary operator ! is the boolean inversion operator, i.e.
!true → false
!false → true
The binary operator != is the "not equal" operator, i.e. the boolean inverse of "equal to"
a != b → true <=> a == b → false
or written slightly differently
(a != b) == !(a == b)
Take note of putting the boolean inverse operator in front of a pair of parentheses. The reason for that is, that the boolean inversion operator has a higher precedence than the boolean equality operator. Which means that !a == b is equivalent to (!a) == b.
In C and by extension C++ every nonzero value is considered boolean true, and zero is boolean false. Also the result of boolean operators is specified to be either 0 (→false) or 1 (→true).
Now let's look at your two different expressions "!a==-1 and a!=-1"
Since ! is a boolean operator the result of !a is defined to be either 0, or 1. So in case a is nonzero, i.e. true (!a) → 0 thus 0==-1 → false as for a being zero (!0) → 1 thereby 1==-1 → false. Hence the expression !a==-1 will always yield false.
a!=-1 on the other hand is comparing the value of a with -1. Its boolean algebra equivalent would be !(a==-1).

assigning boolean values into boolean variables [duplicate]

This question already has answers here:
using Boolean to represent integers
(4 answers)
Closed 7 years ago.
int x = 5;
bool t = ((x % 3) && (x % 4));
when I try this code in visual studio, I get the value true for variable t. How this code is working?
First of all, you have the && operator, which compares two values and returns a boolean, so t will b a boolean. Secondly, in C++, every value other than 0 will evaluate to true, so x % 3, which evaluates to 2 will return true, then x % 4, which evaluates to 1 will also return true.
So in the end you have
bool t = (x % 3) && (x % 4);
which equals
bool t = 2 && 1;
which equals
bool t = true && true;
which equals
bool t = true;
All nonzero values are considered as true for logical operations like AND(&&) and OR(||)
x%3 -s a module operation returned value 2,
x%4 returns 1
2 && 1 is equal to true && true , which is equal to true.
All non zero value assignment to a bool typed variable is considered as true or 1 and all zero value assignment considered as false or 0.
Operator % returns the remainder after division (modulo) and true is any value that is not 0.
5 % 3 == 2
5 % 4 == 1
2 && 1 == true && true == true
you can just do:
bool t=(x% 3!=0) && (x% 4!=0);

What does the line x=!y mean

Here is a part of code
int main()
{
int x=5,y=10;
if(x=!y)
{
cout<<"h";
}
else
{
cout<<"p";
}
getch();
}
The output was p, please explain, how the code works and the meaning of x=!y.
Looks like a typo that produces valid code. Expanding it helps--
if (x = (!y))
Since y is 10, !y == 0, and assignments themselves produce a value. In particular the value of x = 0 is 0, so the test evaluates to 0 and that's why you get the result.
But this is a crazy thing to write in this context, presumably what was, or what should have been intended was
if (x != y)
I.e., not-equals.
x=!y is an assignment.
x is being assigned the value of !y expression, which is a logical "NOT" operation. This operation returns true if the operand is zero, or false otherwise. The value true becomes 1 when assigned back to int; false becomes zero.
In C and C++ it is OK to use assignment expressions inside if conditionals and other control statements, such as for and while loops. The value being assigned is used to evaluate the condition, and the assignment itself is performed as a side effect. In this case, the condition is !y.

How are these evaluated as true?

Consider the following examples:
int x;
if (x = 1)
//...
if (std::cout << "Is it true?")
//...
Both are evaluated as true, but why?
The if (x = 1) is essentially the same as tmp = (x = 1); if (tmp) ...
The "result" of operator << (const char *) is an ostream&, which has an operator bool that the compiler calls to make it "comparable". operator bool will return "false if there was an error, otherwise true". Before C++11, it would return a void*, but the meaning was the same ("false (= 0) if there is an error, true otherwise".
The result of assignment is its left operand, so the value of x = 1 is its left operand x, which has the value of 1 now. And any non-zero integer is treated as true in the condition.
Because x = 1 is an assignment setting x to 1, not a comparison for equality. Comparison is done via x == 1.
It is a common mistake. Often people write 1 == x to catch these errors, since assignment to a constant triggers a compile-time error.
Besides the assignment result for x as mentioned in the other answers the
if (std::cout << "Is it true?")
evaluates to true, because the statement is equivalent to
if ((std::cout << "Is it true?").good())
which checks for the stream state of std::cout.
Both statements return a value other than 0 and are thus considered to be "true".
If you assign a value to a variable, the result of that statement is the assigned value so you can assign multiple variables at once. For example, foo = bar = baz = 1. This works because baz = 1 yields 1, which is then assigned to bar, then to foo. So x = 1 has the value 1 and is thus true.
Next, the std::cout << "foo": the operator<< on a ostream returns the ostream so you can do things like std::cout << "foo" << "bar";. Again, there is a value returned that is not 0 and thus true.
In this condition x = 1 you firstly assign 1 to your x variable, and then test x, so it is exactly the same with:
x = 1;
if( x ) // which is 1, so true
About the output operator <<, the compiler will return true if the output-stream's state cout is ok. So it is not going to be a valid stream if some error occurs.
In the first one, you're assigning the value of 1 to x. A non-zero value will always evaluate to true in a condition.
For the second one, when std::cout << "Is it true?" is called, operator<< will return a reference to std::cout. At that point, the condition is simply reduced to:
if (std::cout)
//
This is now evaluating whether or not std::cout is in a good state.

In C++ syntax, can the condition of the if else, return a int and still execute the statements within

Here is the code which compiles :
int select_object = 0;
if( select_object ) //condition returns an int
{
printf("Hello");
}
if condition returns an int and not a boolean will the hello be printed ? When I tested this it printed hello.
Any idea why even for an int it executes the print statement.
THanks
In C and C++, any nonzero integer or pointer is considered true. So, since select_object is 0, it should not be printing Hello.
Boolean logic
1 = True
0 = False
1 && 0 = False 0
1 && 1 = True 1
1 || 1 = True 1
1 || 0 = True 1
So the answer is for non-zero it is considered true, for 0 it is considered false. If your value (your int) returns 0 it won't execute. If it returns a value that is not 0 it will execute.
In C or C++, a bool is just a fancy way of saying 'int with special values'. Every logical test (if, while, for, etc) can use an int or a pointer for its test instead of a bool, and anything that isn't 0 is true. NULLs and 0 are equal in this sense.