Go Down

Topic: Problem with DO...WHILE command - Expected Primary Expression Query (Read 1 time) previous topic - next topic

GreyE30

Thanks for the advice, it confirms what I was thinking. So my next question is then, if I make my "while (true)" statement to be whilst pedGreen = HIGH;

Would this then follow for both the "digitalWrite" and "pulseIn" parts of the following code?



Please can you explain further how use of "for" and "while" commands may help the program to run when "do and while" commands do not?

TIA

PaulS

Quote
Both should be equivalent

I don't see that.

while ()
{
 //do something
}
and
for (;;)
{
 //do something
}
are equivalent, but the added () in your example is wrong, as far as I can see.

AWOL

Code: [Select]
while {
}

is still syntactically incorrect, but
Code: [Select]
for(;;) is not.
In one example, an empty expression is assumed to be true always, but in the other, it is not.

OP
Quote
Please can you explain further how use of "for" and "while" commands may help the program to run when "do and while" commands do not?

A "for" or a "while" loop may never execute the body of the loop, but a "do...while" will always execute the body of the loop at least once.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PeterH


In one example, an empty expression is assumed to be true always, but in the other, it is not.


My guess is that due to the flexibility of the for loop, the syntax was chosen to allow each of the elements to be optional. This is unusual, and not something that is allowed for boolean expression in general. For example, this doesn't compile:
Code: [Select]

if()
{
}


Missing out expressions from the for loop is slightly unusual but an experienced programmer should not be surprised to see it.

Leaving out expressions in while, if statements and so on is obviously and glaringly wrong. An experienced programmer seeing that there is an error on that line should immediately realise what is missing, even without the compiler error message pointing to the parenthesis and saying "Hey, you left something out HERE".
I only provide help via the forum - please do not contact me for private consultancy.

AWOL

Quote
the syntax was chosen to allow each of the elements to be optional.

I've got no problem that you can miss out an expression, I was just commenting that a missing expression in one particular case should evaluate to "true" is quirky.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up