I have got these instructions from Arduino;
In Arduino programming, to exit a "while" loop using a "break" statement, you simply include the "break;" keyword within an "if" condition inside the loop, which when triggered, will immediately jump out of the loop and continue executing the code following it; essentially allowing you to stop the loop prematurely when a specific condition is met.
Would this coding do it;
while(a<0){
a;
if a>=0{
break;}
}
the rest of my program here. actually there is no code directly following. it is the closing bracket of the while statement but this must be what is meant.
The code that you posted would, indeed, exit the while loop if the value of a becomes greater than or equal to zero
However, as there is nothing in the while loop that changes the value of a, the likelihood of it becoming greater or equal to zero is very low. Is anything outside of the while loop, such as an interrupt, changing the value of a ?
What is this statement a; intended to do ?
I can't help feeling that we have been here before
the syntax is somewhat approximative in what you posted.
while (a < 0) {
a = someFunctionProvidingAResult();
if (a>=0) break; // will leave the while loop right here
... // more code here
}
but it's a bit weird since your while condition is testing a too against the opposite condition. so if the rest of the code after the if does not change a, you'll enter the while loop again.
May be it would be
while (true) {
a = someFunctionProvidingAResult();
if (a>=0) break; // will leave the while loop right here
... // more code here
}
that is it's an infinite loop and you exit when a becomes positive or null.
if you mean that your code is
while (a < 0) {
... // some code here
a = someFunctionProvidingAResult();
if (a>=0) break; // will leave the while loop right here
}
then the if is useless, let the while test do it
while (a < 0) {
... // some code here
a = someFunctionProvidingAResult();
}
.
always post code in code tags.
Have you tried it? What was the result?
I have been looking closer at the while statement in the help section of Arduino. I now see where I have been wrong. You cannot exit from the while statement with just a; as you suggest.
You can only exit when the EXPRESSION in the variable becomes false. This then is as simple as putting in 'a<0'. This is an expression the truth of which can be tested. 'a;' is not an expression. So that was the reason I could not exit the while statement.
You may have seen this or not? Apparently no one else on this forum saw this or they would have corrected me.
Bob did in the very first response
instead of
you could do
while (getA() < 0) {
doSomething ();
}
or
while (true) {
if (getA() >= 0)
break;
doSomething ();
}
It is difficult to correct something when it is not clear what is expected. What did you expect a; on its own to do ?
@gcjr Do you really think that this code-snippet without any explanation will help user @petercl14 ?
Did you finally understand that user petercl14 has almost zero knowledge, is a slow learner because she/he is quick in her/his mind with (mostly wrong) assumptions and is hard to convince from what is really happening?
You left out the definition if your function getA(). Why should such a incomplete information help at all??
@StefanL38 - please take your own advice
[Be the change you want to see in the world.
This notion of be the change you want to see in the world does 3 powerful things when we adopt it:
It stops us from judging others;
It replaces complaining about others with reflection on self;
It stirs us into taking action within the only thing in the world over which we have any control: ourselves.](Profile - StefanL38 - Arduino Forum)
answer to gcjr
I was asking you questions. I wrote my opinion about your post.
OK. To start reflecting about myself:
I have posted many times my opinion about gcjr posts.
I have observed a small change in your posts.
My above question has a rhetoric style. It is a question. The style of the question implies
My question has a rhetorical style. It is a question but the question contains a judgement as to whether it is helpful to the threadopener or not. The wording suggests that I consider the post unhelpful. So it is a judgemental comment about your post. I was judging what you did. NOT your person. The aim of this comment is indeed to trigger a change in the way you post. I have not (yet) succeeded in doing so. Now I could ask myself: how would I have to behave in order for this change to occur in you? I don't have an answer to that. I don't know. I could simply choose not to react. I don't want to do that. I specifically want the communication to improve.
That contradicts this statement:
It replaces complaining about others with reflection on self;
It stirs us into taking action within the only thing in the world over which we have any control: ourselves.
What can I do. What ‘actions’ can I take to control MYSELF, to change MYSELF?
At this point, I make a conscious decision not to change ME. I want YOU to change your posting style.
I could revert to not demanding a change but just writing my assessment of whether the post could be helpful or not.
I could do that. It is an unresolvable tension when influencing others does something good.
Extreme example:
An angry person starts lashing out at someone ‘I'll beat you black and blue if you don't shut up’ Should I withdraw to myself. Try to relax, smile mildly?
What would be the reaction accepted by most people? What would be the most morally valuable action? What would be the action that promotes the ‘good’ in people, that increases community and peacefulness?
Not so easy to decide. Maybe I should remove the saying from my profile because this saying does not really consider the complexity of real life.
I expected that the variation of the value of 'a' in another part of the program would let me exit the 'while' statement. I see this is wrong now. The Arduino help instructions say that you can exit the 'while' statement when the expression in the variable becomes false. 'a' on its own is not an expression in the variable. But 'a<0' is.
The correct code may be;
while(a<0){
a<0;
}
This is even simpler than using a 'break' and an 'if' statement to exit a 'while' statement.
'a' of course is a global variable so that the value assigned to 'a' in an earlier part of the program will be seen in 'a<0'.
The meaning of "expression in a variable" is pretty vague. I hope this is correct on testing.
About while
A while loop will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false.
About break
is used to exit from a for, while or do..while loop
Where do you see "you can exit the 'while' statement when the expression in the variable becomes false"?
And what, exactly, does this code do? and to what is it the answer?
while (a < 0) {
a<0;
}
[quote="petercl14, post:14, topic:1351922"] 'a' on its own is not an expression in the variable [/quote]
I have no idea what you mean, but a variable standing by itself is an expression. not much of one, I grant you, but perfectly valid.
a7
We all saw it but it was not the elephant in the room…
Also it could have been a thing… you never know…
For example this compiles and does something and you’ll recognize your weird while loop…
int x = -31;
#define a (++x)
void setup() {
Serial.begin(115200);
while (a < 0) {
a;
if (a >= 0) break;
Serial.println(a);
}
}
void loop() {}
Of course it’s really bad coding…
It seems to me that you have a misconception on how code runs on small monocore MCUs. It’s not like excel where forumulas get evaluated in all the cells and a change somewhere drives some change elsewhere - eg you have your while loop with a condition based upon the value of a variable and that variable changes somewhere else in the code (not in an interrupt routine) then as long as you are in the while loop, the rest of the code is not executed
The problem is that with the code is stuck in the while loop no other part of the program is running. There are ways round this but I feel that you are not ready to use them, nor are they really applicable to most sketches
no it is not
for the same reasons stated more than one time above
because the value of a does not change.
there needs to be some mechanism that affects the value of a such as a computation performed either within the loop or in an function returning a value
while (getA() < 0)
or possibly reading a new value
while (analogRead (A0) > 200)
From what you say then Bob it would seem impossible to get out of a while loop. Any chance of you giving me these other ways.
What about the suggestion by gcjr to use a function. Basically with this you are calling a subroutine and in this you will find the value of 'a' and return the value to the 'while' statement. Does that sound feasible?
It is suggested to use an 'analog read' in the 'while'. But wouldn't this also be in another part of the program "code stuck in while loop no other part of program is running".
I think you are being deliberately obtuse and needlessly argumentative.
If you have a specific requirement to exit a while loop, spell out your problem and we can help.
If you are just trying to learn C or C++, this is not good way to do it.
a7
It isn't impossible. See e.g. while Loop in C - GeeksforGeeks. And if you want non-blocking code, you should adjust the program and use an if statement.
I suggest that you start reading a book or some tutorials.