If statement without condition

Hello,

I was playing around with trying to measure water level using two wires. I have a 100 kOhm resistor connected between A0 and ground. I have one wire attached to A0, and one to the Arduino 5 V output. This is to make a simple voltage divider circuit. The idea is then that when the two wires (one connected to A0 and one connected to the 5 V output) are in tap water, some level of resistance will be measured and I can use this to detect if water in my container has dropped below a critical level. Obviously the resistance will vary depending on electrolytes in the water, distance between the wires, etc. However, I was simply planning on putting a very high threshold for resistance so that if any resistance is measured (e.g. just not a broken circuit), I could trigger some code.

Here is what my original code looked like:

int analogPin= 0;
int raw= 0;
long threshold = 10000; //I was planning on changing this as required.


void setup()
{
Serial.begin(9600);
}

void loop()
{
raw= analogRead(analogPin);
if(raw < threshold)
{
Serial.println(raw); //For now I am just printing the result. I put the wires in the water, and see if I get anything printed to the Serial monitor. I would of course change this code to do what I need when the water level drops too low.
}
delay(1000);

}

However, as I started playing with the “threshold” value, I eventually (actually by mistake) tried this:

int analogPin= 0;
int raw= 0;
long threshold = 10000; //I was planning on changing this as required.


void setup()
{
Serial.begin(9600);
}

void loop()
{
raw= analogRead(analogPin);
if(raw)
{
Serial.println(raw); //For now I am just priting the result. I put the wires in the water, and see if I get anything printed to the Serial monitor. I would of course change this code to do what I need when the water level drops too low.
}
delay(1000);

}

The difference here is that I have an “if” statement, but no condition. Surprisingly (for me at least…) this code works perfectly. I have found that this code knows when the wires are in the water, and when I remove them. Since I really don’t care what the absolute resistance values are, this is perfect for me. After this long background, my questions are:

  1. What does the if statement look for when I put no condition in parenthesis?
  2. When the wire from A0 is not in water, isn’t the value just floating? If so, why does it not occasionally trigger my code? I have left this for quite awhile, and when not in water, the code has not been triggered.

I appreciate any input.

Thank you,

Dustin

The if evaluates the expression in parenthesis to see if it is true or false. False being zero and true being not zero. So if raw is non zero the if evaluated to true.

The difference here is that I have an "if" statement, but no condition.

You DO have a condition. It is just that you have different expectations for what defines a condition that the compiler, and the rest of us, have.

An "if" has a condition, which is an expression. An expression can be a comparison. It is a comparison that you are thinking of.

Thank you all for your replies. Based on what groundfungus said, would writing something like "if(raw)" be the same as writing "if(raw>0)"?

Thank you,

Dustin

Yes.

No, raw could be negative it should still evaluate to true.

It would be the same as:

if(raw != 0)

What does the if statement look for when I put no condition in parenthesis?

Actually it is the job of the compiler to evaluate the “condition”, not the job of “if” statement.
Just try this

if(){}

and check the compiler error output.

And yes, the condition is evaluated to true or false
so

if( a == 0 ) and if(!a) are same as far as compiler is concerned.

And as a bonus - if you use if(!a) you can avoid common coding mistake of using assignment instead of evaluation, such as

if(a=0)

Good point Vaclav,

That can also be avoided by using

if (0 == a)

// const first, ==> an assignment typo would fail

Thank you all. This was extremely helpful for me.

TwoChain: Thank you all for your replies. Based on what groundfungus said, would writing something like "if(raw)" be the same as writing "if(raw>0)"?

No. it is the same as writing "if(raw!=0)" .

Incidentally, because LOW is defined as 0, you can also if(digitalRead(PIN)) .

PaulMurrayCbr: Incidentally, because LOW is defined as 0, you can also if(digitalRead(PIN)) .

Somewhat misleading , simplistic , statement giving the impression that such "syntax" / usage ( not really a syntax, but I cannot find a better word to express this ) is limited to if(digitalRead(PIN)).

Risking the flaming for " do not confuse beginners", the if "condition" is now a returned value from a function digitalRead(PIN).

I would just add that the "condition" is not limited to simple stuff, but is basically limited by programmer's skills and imagination.

But KISS is always better anyway.