Question

I'm another newb coding in c++ for the nds using devkit pro and I couldn't find anything online about this, surprisingly. How do I code an or statement like the following:

if ((keys & KEY_L && LR==true) or (keys & KEY_X && LR==false))
{
    ...
}

If you wanted to say, "if L key is pressed..." you would use "if (keys & KEY_L)..."

So how would I say "if LR is true, and the L keys is pressed, or if LR is false, and the X key is pressed"

Was it helpful?

Solution

LR == true (mind the double equal sign, otherwise you are writing an assignment instead of a comparison!) is unnecessary: just use LR. Similarly, use !LR instead of LR == false. Also, adding a couple of parentheses won't hurt:

if (((keys & KEY_L) && LR) || ((keys & KEY_X) && !LR))
{
    ...
}

OTHER TIPS

Almost as you have done, just use == for equality comparison:

if ((keys & KEY_L && LR==true) || (keys & KEY_X && LR==false))
{
    ...
}

While or will work, it is merely an alternative token for ||. Additionally, you can change LR==false to just !LR.

You're close. For some bizarre reason, the && and || operators have higher priority than | and &, so you need a few extra parenthesis:

And you need to use == to check for equality.

Change this:

if ((keys & KEY_L && LR=true) or (keys & KEY_X && LR=false))

to this:

if (((keys & KEY_L) && LR==true) or ((keys & KEY_X) && LR==false))
  1. learn the difference between & and &&.
  2. learn the difference between == and =.
  3. learn how if works. if(val == true) is just the same as if(val)...
  4. use parenthetical groupings to make smaller boolean expressions and build them up.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top