IF : mixing &&, ||, etc ... in the same condition

Hi all,

As i'm a noob on programming, i would like to know if it is possible to mix conditions that are differents in the same IF sentence ?

like :

if(A > B && A < C || D > E){
do something
}

Is it possible to use parenthesis to do the trick ? like :

if((A > B && A < C) || D > E){
do something
}

Thanks !

Yes. You can check this to see if parens are required, or you can include them for clarity even when not required.

http://www.difranco.net/cop2220/op-prec.htm

It is possible, and your examples look fine. Parentheses are a good idea to make the meaning clearer.

Required reading:

Thank you all !

I'm actually running a greenbox and i'm programming the weather management of the inside of the box.

Here is the main code :

void WeatherDayControl(){
  // Conditions for ENABLING aeration
  if(ActiveAerationState == 0){
    if((dht_IN_Temp >= Temp_IN_Value + Temp_IN_Value_Threshold && dht_IN_Temp - dht_EXT_Temp >= 5) || dht_IN_Hum - dht_EXT_Hum <= -15 || millis() - ActiveAerationPreviousStateChangeMillis >= ActiveAerationOffDuration){
      ActiveAerationOn();
    }
  }
  // Conditions for DISABLING aeration
  if(ActiveAerationState == 1 && millis() - ActiveAerationPreviousStateChangeMillis >= ActiveAerationOnDuration){
    if((dht_IN_Temp <= Temp_IN_Value - Temp_IN_Value_Threshold && dht_IN_Temp - dht_EXT_Temp <= 2 ) || dht_IN_Hum - dht_EXT_Hum >= 5){
      ActiveAerationOff();
    }
  }
}

And the 2 functions :

void ActiveAerationOn(){
  if(ActiveAerationState == 0){
    ActiveAerationPreviousStateChangeMillis = millis();
    ActiveAerationState = 1;
  }
  FanOUTon();
  FanINon();
  ServoOUTopen();
  ServoINopen();
}
void ActiveAerationOff(){
  if(ActiveAerationState == 1){
    ActiveAerationPreviousStateChangeMillis = millis();
    ActiveAerationState = 0;
  }
  ServoINclose();
  ServoOUTclose();
  FanINoff();
  FanOUToff();
}

The behavior has to be :
To execute the ActiveAerationOn(), conditions are :
(TemperatureIN >= TemperatureREF + TemperatureTHRESHOLD [AND] TemperatureIN - TemperatureEXT >= 5)
[OR]
HumidityIN - HumidityEXT <= -15 % Relative Humidity
OR
millis() - ActiveAerationPreviousStateChangeMillis >= ActiveAerationOffDuration // Duration since last turn OFF state to be respected

To execute the ActiveAerationOff(), conditions are :
FIRSTLY : millis() - ActiveAerationPreviousStateChangeMillis >= ActiveAerationOnDuration // Duration since last turn ON state to be respected
THEN
(TemperatureIN <= TemperatureREF - TemperatureTHRESHOLD [AND] TemperatureIN - TemperatureEXT <= 2)
OR
HumidityIN - HumidityEXT >= 5 % Relative Humidity

Do you think everything will run smoothly with my code ?

Feel free to upgrade this little piece of s... :wink:

Thanks !