Understanding & in an if statement

Hi everyone,

I am working on modifying some code for a control system at work. The guy who initially wrote the code retired a while back and is no longer available for contact. I was wondering if anyone could help me understand the following statement:

if (nValueY >= -100 && nValueY <= 100 && nValueX >= -100 && nValueX <= 100)
{
nJoyY = 0;
nJoyX = 0;
}

The only thing I don't understand from this line is the double &&

Can someone please help me understand this?

This is the standard C/C++ logical AND operator:

https://en.cppreference.com/w/cpp/language/operator_logical

Your code got a bit mangled - I think you meant this?

if (nValueY >= -100 && nValueY <= 100 && nValueX >= -100 && nValueX <= 100)
{
   nJoyY = 0;
   nJoyX = 0;
}

It's wise to use parentheses, as there are some common pitfalls with precedence here:

if( (nValueY >= -100) && (nValueY <= 100) && (nValueX >= -100) && (nValueX <= 100) )
{
   nJoyY = 0;
   nJoyX = 0;
}

So that's checking that both the values are in the range -100..+100 (inclusive).

That is actually how the code is written exactly how I am looking at it which is probably why I am so confused. (I didn't write it, I'm just trying to modify it and clean it up a bit)

Just to reiterate, to make sure I am understanding the last 2 replies correctly:

&amp;&amp;

is just && correct?

(also I apologize, I didn't realize the forum was already changing my &amp to a & automatically)

Thank you so much for your help

Right, just &&. In forum posts please use the < CODE> tag to prevent changes to the entered code.

Yes, that's right.

Note that the '<' (less than) and '>' (greater than) had also been mangled.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.