Two nested if functions

Hello,

Probably a simple question:

How do I nest two if functions?

I've tried:

if (state & SNES_SELECT) 
  {
      If (ServoPos1 = 2100)
      {
       // Do something
      }

      Else
      {
       // Do something
       }
  }

But this doesn't work.....

Thanks in advance :slight_smile:

Without knowing what types of variables state and SNES_SELECT are, I would first assume that you need to use '&&' and not the '&'

Hello,

if (state & SNES_SELECT)
  {
      // Do something
  }

This code works great. What is the difference between & and &&.

The error I get when using my nested code:

In function 'void loop()':
error: 'If' was not declared in this scope

Wow, I didnt even see that: reserved words are case sensitive. 'if' is not the same as 'If'. This same thing has gotten me lately coming back to c++ from Delphi

& is a bitwise 'and'
&& is a logical 'and'

You're missing some basis on C programing.

& is "bit and" between to value for example (int)foo & 0xF will mask the high 4 bits of foo variable

&& is "logical and" it is used to combine logical values (true or false) for test purpose for example.

Ok got it thanks :slight_smile: . I am known with Visual basic and pretty new into C++.....

Another question:

How can I do this? This example is VB

if z=10 AND x=20 then

end if

This would save me a whole lot of code.

Thanks in advance.

if (z == 10 && x == 20) then
{
//do some stuff
}

or

if (z == 10) && (x == 20) then

dont forget that '==' is a comparison, and '=' sets a value