Can i know which of the statements were true in an if statement with OR inside of it?

In the case below, can i know or use the information of wich statement was true?

if(number == 1 || number == 2){
/*other code*/
}

Sure. Test each individually before the compound test.

Thank you, but im working on a bigger project and i am worried of my UNO's program storage space. I would like to shorten my code. do i have any other options?

Both conditions could be true and you would never know from your logic.

1 Like

You can comment or remove the test code after you get satisfactory results of the test(s).

Serial prints are the best debugging tool that you have with the Arduino IDE.

Your math seems to work differently from mine. Where I come from, an integer can only be either "==1" or "==2", not both at once...

Thank you for the answers, but just to be clear.

if(y[x] == z || y[x+1] == z){
          y[/*i cant code it to write here x or x+1 depending on which was true*/]--;          
        }

It depends. If both are equal to z do you want to decrement both? If so then you will need to do this:

if(y[x] == z)
{
   y[x]--;          
}
if(y[x-1] == z)
{
   y[x-1]--;          
}

There might be a more elegant solution but without knowing your application or the rest of your code I can't say.

Sure, but the logic is faulty is the actual value is important.

if (number == 1 || number == 2) {
  Serial.println(number);  // print the number 
  if (number == 1) {
    // do something
  } else {
    //number = 2, do something else
  }
}

Might want to change your logic if the number is a float.

If you describe your project in detail more suggestions can be made
Without more information everything is just guessing.

If you use a lot of text-out to a display all constant textes can be put into flash by using the F-macro.

Though I don't know if you do a lot of text-output I can't really say.

depending on your budget you could buy a different microcontroller with more flash and RAM-memory. An Arduino Mega

if you need just a smaller number of IO-pins or using an I2C-Io-expander is sufficient you could use a $8 cheap Seeeduino XIAO which has 16 times more RAM than an arduino Uno
though I don't know the details I can't say

If you plan to have internet-connectivity using an $10 ESP32 will deliver internet-connectivity combined with plenty of RAM
though I don't know the details I can't say

etc. etc. etc. etc

best regards Stefan

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