Hi.
In a piece of code I have found this:
if(g_count++ == 20 || g_SendMqttData)
g_count = 0;
I don't understand what exactly means:
++ == ||
May you help me?
Thanks in advance
Hi.
In a piece of code I have found this:
if(g_count++ == 20 || g_SendMqttData)
g_count = 0;
I don't understand what exactly means:
++ == ||
May you help me?
Thanks in advance
exactly what is writen:
++ is increment operator
== is equal operator
|| is OR operator
As @Juraj said they are standard C/C++ operators:
as distinct from the assignment operator, =
Ok.
Then translated in "human" language what happens in this piece of code ?
if(g_count++ == 20 || g_SendMqttData) // Every 10 [sec] or if input status changed
{
g_count = 0;
////////////////////////////////////////////////////////////////
// pin32 value (intero)
////////////////////////////////////////////////////////////////
g_pin32Value = analogRead(Pin32);
Serial.print("Valore su Pin32 (intero): ");
Serial.print(g_pin32Value);
Serial.println(" numero");
g_SendMqttData = true;
}
I think you need some time with a C++ (or C) textbook; eg, the one I linked earlier.
for an actual printed book, "K&R" is the standard reference; eg,
++ is the increment operator; ie, it adds one.
When placed after a variable, it is called a post-increment, because the increment is applied after the variable's value has been used
Thus the expression g_count++ == 20 will evaluate to true if the value of g_count is equal to 20, and false otherwise, and after this the value of g_count will be incremented by one.
it say if g_count is equal to 20 OR if g_SendMqttData is true (or non zero if it's an integral) then do what's in the if section
and after that g_count has been increased (but because the ++ is after g_count, it's the value prior to the increment that is used for the compare)
Check the complier warnings too.
Beware that having a load of comparison operators like this can give unexpected results - unless you're absolutely sure of the precedence & association rules, it's wise to use brackets to be sure; eg,
if( (g_count++ == 20) || g_SendMqttData )
And brackets are now available free of charge.
The IDE sections are for problems with the IDE. You don't have a problem with the IDE and hence your topic has been moved yo a more suitable location (Programming Questions) on the forum.
There is bitwise OR which is || and there is logical OR which is |.
edit - add: Yes, I got it backwards and I don't know why but I know where my keys are!
Instant Help links.
The Arduino Reference (bookmark!) to look up answers you don't have to wait for.
the opposite
bitwise is |
logical (boolean algebra) ||
No - it's the other way around:
| is bitwise;
|| is logical.
You're right! I need to spend more time writing!
if(g_count++ == 20 || g_SendMqttData)
nothing complicated:
if g_count incremented by 1 is equal to 20 or g_SendMqttData is not zero then
EDIT: g_count is incremented by 1 after the comparison with 20
Are you sure
See post #7.
If g_SendMqttData could == 2 then that would force a non-zero result.
The code may have gone beyond dead simple already.
What I expect to happen:
First the equation is evaluated, g_count == 20 gives a ‘true’ or ‘false’.
Then g-count is incremented.
Then g_SendMqttData is evaluated, typically a 0 gives ‘false’, anything nonzero is regarded as ‘true’.
Then these two are OR’ed, when ‘true’ the next statement is executed.
Some programmers like to use as little characters as possible in their statements.
actually the left part of the test is evaluated, so g_count is incremented but the expression returns the previous value of g_count and then the comparison is done.
(the order of evaluation for operands of an operator is not specified by the standard, and it can vary based on the compiler and compiler optimization settings)