Question about while(1) and it's application results !

hello everyone,
i came across an example, in which they use while(1); to break the main loop,
well i need please to understand how while(1) broke the main loop and stopped it from continue again and again ?
we know that when we use codes inside while(1) or while(true) the codes will repeat for ever till the memory is full or whatever,
i guess in arduino the while(1) is not needed because the main loop void loop() will repeat forever ?

while(1) {

}

creates an endless loop. It is sometimes used to make a program stop doing anything useful. The only way to stop it is to press the reset button.

It can also be used to create a "loop" within setup() - perhaps because you want to put some code inside the WHILE to wait for something to happen before the program continues from setup() to loop(). In that case you can use the break statement to escape from the WHILE

...R

Robin2:

while(1) {

}



creates an endless loop. It is sometimes used to make a program stop doing anything useful. The only way to stop it is to press the reset button.

It can also be used to create a "loop" within setup() - perhaps because you want to put some code inside the WHILE to wait for something to happen before the program continues from setup() to loop(). In that case you can use the break statement to escape from the WHILE

...R

but how does it stop the program ? or it doesn't ? what exactly happens here ?

here are the codes, it is used inside void loop()

unsigned long start_time = 0;
unsigned long end_time = 0;

#define test_type byte
test_type x = 1;
test_type y = 2;
test_type z = 3;

void setup() 
{
  Serial.begin(57600);
  Serial.println("SparkFun datatype demo.");
  Serial.println("Addition with bytes: x+y=z");
  Serial.print("x=");
  Serial.println(x);
  Serial.print("y=");
  Serial.println(y);
}

void loop() 
{
  start_time = micros();
  z=x+y;
  end_time = micros();
  Serial.print("z=");
  Serial.println(z);
  Serial.print("Start time: ");
  Serial.println(start_time);
  Serial.print("End time: ");
  Serial.println(end_time);
  Serial.print("Elapsed time: ");
  Serial.println(end_time - start_time);
  while(1);  
}

There is no way to stop program. Running is endless

Your loop() function will exute once, an then keep evaluating while (1) endlessly.
You can move your code in the setup() function and keep an empty loop() function with the same result

The code you've posted ends on the while(1); statement in infinity loop. The loop() function runs only once and never ends. The only thing which will be executed beside of the main code is any ISR so e.g. timekeeping for microseconds or milliseconds will be updated as expected.

The confusion could easily be ended by putting all the code that's in the loop() function into setup(). Just leave an empty loop:

void loop() {}

I've seen a lot of demos where the writer thought putting everything in loop() was magically necessary and had to use that while(1) or something like that. Just muddy thinking.

firashelou:
hello everyone,
i came across an example, in which they use while(1); to break the main loop,
well i need please to understand how while(1) broke the main loop and stopped it from continue again and again ?

The expression '(1)' evaluates to 'true' and the meaning becomes perhaps better visible with this code:

while (true==true) ; // as long as "true==true" ==> execute the empty do-nothing command ";"

This will be the same as "forever execute the do-nothing-command ';'.

thank you guys for reply,
now i understand it so it simply keep executing an empty while and never stops that's why the first couple of lines are visible one time

Budvar10:
The code you've posted ends on the while(1); statement in infinity loop. The loop() function runs only once and never ends. The only thing which will be executed beside of the main code is any ISR so e.g. timekeeping for microseconds or milliseconds will be updated as expected.

what do you mean keep updating ?! because the time in the code i posted is fixed and never update, even if i close the monitor and re open it stays the same value, or if i reset of course !

I meant timekeeping variables as an example. They are updated in ISR, e.g. there is internal variable timer0_millis which keeps milliseconds and it will be continuously updated even though the main program hangs. Of course it cannot be seen, but imagine the led blinking in ISR. LED will be blink.

Budvar10:
I meant timekeeping variables as an example. They are updated in ISR, e.g. there is internal variable timer0_millis which keeps milliseconds and it will be continuously updated even though the main program hangs. Of course it cannot be seen, but imagine the led blinking in ISR. LED will be blink.

ah ok, just in ISR right ? because if i blink light and after put delay(500); then while(1) then the LED will stay of till i reset the program right ?

Yes, in ISR. Let say just writing the 0 or 1 to pin inside of timer ISR is enough.

Budvar10:
Yes, in ISR. Let say just writing the 0 or 1 to pin inside of timer ISR is enough.

ok thank you :slight_smile: