0

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"

Matthew D. Scholefield
  • 2,977
  • 3
  • 31
  • 42

4 Answers4

2

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))
{
    ...
}
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
0

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.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

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))
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
-1
  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.
djechlin
  • 59,258
  • 35
  • 162
  • 290
  • Downvoted because this is NOT an answer - should be a comment. Could easily be edited to a valid answer though. – JBentley Mar 04 '13 at 20:21
  • @JBentley "use parenthetical groupings to make smaller boolean expressions and build them up." -- answer. 1-3 are recommended as prereq exercises. – djechlin Mar 04 '13 at 20:24
  • @JBentley that is incorrect, his use of `or` is fine - `or` is allowed in C++ and is the same as `||`. As such the only problems I could see were his use of `&`, `=` and building up a larger statement. – djechlin Mar 04 '13 at 20:28