I don't quiet grasp them and would like to know there equivalents in normal/simple code please:
if (inString [inCount] == EndMarker) break; // it looks like the if condition is closed here
{ // but then it starts with an '{' ???
(++inCount < inLength); // and this looks like another condition but??
// I know 'inCount++' but what is '++inCoiunt'??
inString[inCount] = 0; // null terminate the string (this one i understood :-))
}
Q1: How come the if line is terminated with a ';' and the following line is started with a '{' ?
Q2: What is the simple equivalent of the line: '(++inCount < inLength);'
I posted the question on its relevant page but it didn't go through since it is a quiet old subject. so it thought I seek advice in a new topic, hoping someone can answer this.
Thanks.
if (inString [inCount] == EndMarker) break; // it looks like the if condition is closed here
It makes more sense if you lay it out like this
if (inString [inCount] == EndMarker)
{
break;
}
I know 'inCount++' but what is '++inCoiunt'??
inCount++; increments the value of inCount after its value is used in the expression
++inCount; increments the value of inCount before its value is used in the expression
but still my first question: is about ending the if condition line with a ;
The if condition does not end with a semicolon. It is the statement to be executed when the test returns true that ends with a semicolon.
and starting with the {do something code} as if the if condition is still open.
I believe that this is the section of code that you are looking at
do {
inString[inCount] = Serial.read(); // get it
if (inString [inCount] == INTERMINATOR) break;{
(++inCount < INLENGTH);
inString[inCount] = 0; // null terminate the string
}
}
while (Serial.available()); // wait for input
Here it is reformatted with added comments
do
{
inString[inCount] = Serial.read(); // get it
if (inString [inCount] == INTERMINATOR)
{
break;
}
{ //not needed
(++inCount < INLENGTH); //brackets not needed
inString[inCount] = 0; // null terminate the string
} //not needed
}
while (Serial.available()); // wait for input
And what about
(++inCount<inLength);
is it equivalent to?
if (inCount<inLength) inCount++;
No it is not equivalent, but the original code does not look it works anyway. It shows all the signs of having been revised without thought, hence the extra braces and brackets
Even if it were a "higher level of coding" my advice would be to initially use coding that you understand even if you think it looks ponderous. The rule should be "get it working then improve it"
There is no such thing as a silly question. If you don't understand something then just ask, as you did.
UKHeliBob:
The rule should be "get it working then improve it"
This is a great rule that fully supports the famous SSS Strategy Rule. Always start with small thing (the basic = simple and elementary); make it work; add new thing with it; make it work and so on..
It has been highly pleasing to see the simplification of: if (inString [inCount] == EndMarker) break; into --
In which universe is the adding of braces a simplification?
It is in the conceptual world!
The meaning is well clarified here (to me): if (inString [inCount] == EndMarker) break;. Yet, the OP could not follow it; because, it was not simple enough for him. Reformatting helped the OP to follow the full meaning.
Sorry, no code tags or otherwise (iPad?), but this line:
(++inCount < INLENGTH); //brackets not needed
just is stupid. It increments inCount. Period. INLENGTH don't enter in to it. The expession does have a value which is discarded, computing it is pointless and one hopes optimised away:
No, it's a clarification, nothing in the code is made simpler.
GolamMostafa:
The meaning is well clarified here (to me): if (inString [inCount] == EndMarker) break;.
Yet, the OP could not follow it; because, it was not simple enough for him.
Whandall:
No, it's a clarification, nothing in the code is made simpler.
No, it was too simple for him.
Simple for you maybe but complicated for the OP.
He was obviously aware of the problem caused when an if test is terminated with a semicolon. Splitting the code into several discrete lines and putting the code block in curly braces made the code simpler to understand but did not, in fact, simplify the coding
Come on guys, I am not that much of a beginner in coding, but I only thought I was discovering a new way of an 'if-else condition' syntax and maybe also a new conditional increment format that I have not seen before, but as correctly described by UKHeliBob, these were no more but some un-necessary confusing left-overs .