if statement syntax

which way is correct,

if (digitalRead(FloatSwitch) == HIGH && (TimerState) == true) {
if (digitalRead(FloatSwitch == HIGH) && (TimerState) == true) {
if (digitalRead(FloatSwitch == HIGH && TimerState == true)) {

or

if (digitalRead((FloatSwitch == HIGH) && (TimerState) == true )) {
if (digitalRead((FloatSwitch == HIGH) && (TimerState == true ))) {

all three compile but only 1 works i need to compare both floatswitch and timerstate

They do different things.

It appears that you are trying to use two different conditional tests, and only do the if statement when both of them are true.

So, condition 1 is the pin named FloatSwitch is HIGH, and condition 2 is that a boolean variable named TimerState is set to true.

Write condition 1 like this: digitalRead(floatSwtich) == HIGH
Write condition 2 as TimerState

  • note: you don’t need to say ‘== true’ because it’s already a boolean that can only be true or false.*

Now put the first one in parenthesis since it’s made of multiple operations, and also because you don’t want to wade into the precedence swamp. Then make a new boolean value that is true if both condition 1 and condition 2 are true. So try this:
  if ((digitalRead(floatSwtich) == HIGH) and TimerState) {

  • another note: and is the same as &&.*

while all compile, none are (strictly) correct, although Option 1 is closest

Option 2: Think to yourself, what does digitalRead(FloatSwitch == True) do? Evaluate what's in the brackets and then think of what pin(s) you will be reading (HINT: there are a choice of 2)

Option 3: Again, look at the expression. digitalRead(FloatSwitch == HIGH && TimerState == true)
Evaluate what's in the brackets and then think of what pin(s) you will be reading

You want to logical AND 2 conditions. Make sure the if expression is structured in such a way that you are ANDing the correct conditions

if (condition_1 && condition_2)

use brackets around the entire conditions

FloatSwitch == HIGH

Depending on the value of FloatSwitch, the above will evaluate to true (1) or false (0).

So e.g.

digitalRead(FloatSwitch == HIGH)

will either read pin 0 or pin 1.

Sounds like a homework or exam question :smiley:

ChrisTenone:

  • another note: and is the same as &&.*

If you say and is the same as &&, then which and? will be equal to &? The symbol & is used when the operands are integers and && used when the operands are expressions.

thankyou okay so then this would be correct for this if statement?

if ((reading < 8.59) && (TimerControl) && (Delay != 0 )) {

notsolowki:
thankyou okay so then this would be correct for this if statement?

if ((reading < 8.59) && (TimerControl) && (Delay != 0 )) {

Can’t say if that’s right or not, it depnds on what you want it to test. The if-statements will happen if the double (or float) variable reading is less the number 8.59; AND the boolean variable named TimerControl is true; AND a numeric type (int, byte, etc.) variable named Delay is not equal to zero.

Just learn to read the statements like a western sentence - from left to right.

notsolowki:
thankyou okay so then this would be correct for this if statement?

if ((reading < 8.59) && (TimerControl) && (Delay != 0 )) {

To me, the above structure is not wrong under the following understanding:

float reading;       //non integer
bool TimerControl;         //non integer? or integer
unsigned long Delay;      // integer

void setup()
{
  if ((reading < 8.59) && (TimerControl) && (Delay != 0 )) 
  {
    
  }
}

void loop()
{

}

notsolowki:
if ((reading < 8.59) && (TimerControl) && (Delay != 0 )) {

Don’t be lazy :wink: If TimerControl is a boolean, compare it against a boolean (true or false).

if ((reading < 8.59) && (TimerControl == true) && (Delay != 0 )) {

From your code snippet, you will not be able to determine the type of the TimerControl variable. This way, you can immediately see that TimeControl is (supposed to be) a boolean and in two years time when you look back at your code, you will not be tempted to modify the statement to compare TimerControl with a float.

When your code grows, TimerControl might be a variable hidden somewhere in e.g. an include file and you will not be able to easily know the type without a search through all the source files.

sterretje:
Don’t be lazy :wink: If TimerControl is a boolean, compare it against a boolean (true or false).

if ((reading < 8.59) && (TimerControl == true) && (Delay != 0 )) {

From your code snippet, you will not be able to determine the type of the TimerControl variable. This way, you can immediately see that TimeControl is (supposed to be) a boolean and in two years time when you look back at your code, you will not be tempted to modify the statement to compare TimerControl with a float.

When your code grows, TimerControl might be a variable hidden somewhere in e.g. an include file and you will not be able to easily know the type without a search through all the source files.

Hmm. I advised OP to do that, so I will have to take your comments under consideration. But to me, any condition has to be a boolean. Don’t comparison operators always result in a boolean value: true or false? And isn’t that what a declaration is for? It says what type a variable is.

To me, it seems intuitive, but perhaps thats just my autodidactic self.

@sterretje,

So you’re saying I should write

if ((a < b ) == true){

That’s just stupid.

Or actually since that is itself a Boolean value perhaps this is what you think:

if (((a < b ) == true) == true){

Or maybe I should go one level more? Where do I stop?

Delta_G:
@sterretje,

So you’re saying I should write

if ((a < b ) == true){

That’s just stupid.

How/why is this so bad? (Is it for unnecessary redundancy?)

Assume:
byte a = 2;
byte b = 3;

a < b is true

Therefore, true == true

byte a = 2;
byte b = 3;
void setup() 
{
  Serial.begin(9600);
  if ((a < b ) == true)
  {
    Serial.print("Fine!"); //prints: fine
  }
}

void loop() 
{
 
}

GolamMostafa:
If you say and is the same as &&, then which and? will be equal to &? The symbol & is used when the operands are integers and && used when the operands are expressions.

The name of the "&" operator is "bitwise and". There is no keyword which replaces it. Using "and" to relpace "&&" is an Arduino thing and is not part of standard C.

You can use "&" on any built-in type and any expression. You can use bitwise-and on floats, but the results will be meaningless. It is most commonly seen on bytes but you could use it with integers if you want to work with more bits.

@MorganS

I learnt in my early C Programming school --

& performs 'bit-wise AND' operation which you have mentioned.

&& performs 'logical AND operation'. For example:
If Ali goes to movie, only then I will go. Using && operator, we can express it as --
if(Ali && GM)
{
//event will happen
}

I saw and in VB6; now, it has come to Arduino.

So why did you ask the question if you already know this?

MorganS:
Using "and" to relpace "&&" is an Arduino thing and is not part of standard C.

I'm not sure about C, but it is part of standard C++.
And the Arduino is programmed in C++.

Delta_G:
Or maybe I should go one level more? Where do I stop?

You may stop when the harddisk or your memory is full.

I gave my motivation.