system
April 24, 2009, 2:42pm
1
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
system
April 24, 2009, 2:48pm
2
Without knowing what types of variables state and SNES_SELECT are, I would first assume that you need to use '&&' and not the '&'
system
April 24, 2009, 2:51pm
3
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
system
April 24, 2009, 2:57pm
4
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.
system
April 24, 2009, 3:40pm
6
Ok got it thanks . 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.
system
April 24, 2009, 4:07pm
7
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