# arduino 'while' statement

#### denison

Joined Oct 13, 2018
252
Hi all; I have included a while statement in my software.
while (a=1023){
}
Serial.println(a);
What should be happening is while the analog read is 1023 you cannot exit the while statement which is what I want. The reading at AO is changing all the time but usually at a low. So what should be happening is that you get a print out of 'a' when exiting the while statement.
Higher up in the program the value of 'a' is shown. There is no print out of 'a' in the serial monitor after the while loop. I conclude from this that there must be something wrong in the while loop so that the program cannot continue.
Has any body got an insight into why the program is not working?

#### jjw

Joined Dec 24, 2013
720
Hi all; I have included a while statement in my software.
while (a=1023){
}
Serial.println(a);
What should be happening is while the analog read is 1023 you cannot exit the while statement which is what I want. The reading at AO is changing all the time but usually at a low. So what should be happening is that you get a print out of 'a' when exiting the while statement.
Higher up in the program the value of 'a' is shown. There is no print out of 'a' in the serial monitor after the while loop. I conclude from this that there must be something wrong in the while loop so that the program cannot continue.
Has any body got an insight into why the program is not working?
Should be: while(a==1023)

#### Vytas Klyvis

Joined Dec 5, 2016
75
Also to be noted is that this loop only functions the way you intended if the value of a is already 1023 when entering the loop. Otherwise it would just skip the loop. If that is not way you want you could try:
Code:
do {
} while (a == 1023);
P.S.
The reason it is not exiting the loop is because the statement a = 1023 always evaluates as true.

#### denison

Joined Oct 13, 2018
252
Should be: while(a==1023)
thanks guys. this was giving me a bit of a headache. I haven't been using arduino for long. I haven't done computer programming for a long time. I have been designing circuits in analog electronics and am now putting my analog designs into microcontrollers. or in other words and to be more precise I am achieving the same result with a microcontroller that I have with my analog circuit.
By doing this you can use less hardware. I have so far with arduino been successful in converting another analog design to a microcontroller solution. This did not use a 'while' statement and my '=' statements were not in the mathematical sense so there was no problem in the software.
I used the arduino forum to try to solve my problem and received no answers at all. I wasn't sure if I could get an answer on this forum but then saw a sub heading of microcontrollers.
This is a first class forum for analog and digital electronics. I congratulate the administrators.

#### sparky 1

Joined Nov 3, 2018
607
Here is an example of a countdown program that illustrates the while loop.
To test it you can visit the online compiler and select language "C" copy paste then hit run.
https://www.onlinegdb.com
**************************************************************************

#include <stdio.h>

int main()
{
printf("Launch in T minus 10 ...\n");

int seconds = 10;
while (seconds > 0)
{
printf("%d...\n", seconds);
seconds--;
}
printf("Ignition!\n");

return 0;
}

Last edited:

#### Davec6505

Joined Sep 15, 2020
1
You could use a timer interrupt to check the analog at set interval and continue with code. This could negate the need for code blocking in a while loop

#### sparky 1

Joined Nov 3, 2018
607
Sorry I am a litttle rusty but I know this type programming is where it's at. My example was shameful.
Could you fix it up a little and paste the revision here where it can be found later ? TNX