How to exit loops

Here is a my sketch in general:

void loop() {
  while (...) {
    if (...) {
         ...
    } else if (...) {
         ... 
   } else if (...) {
         ...
   }
   if (counter == MAX) {
      counter = 0;
   }
 }

 for (i = 0; i < MAX; i++) {
    if (...) {
         ...
    }

    if (...) {
        ...
   }

  if (...) {
       ... 
   }

  if (...) {
        ...
  }
 } 
}

I somehow would need to make if anywhere in:

 for (i = 0; i < MAX; i++) {
    if (...) {
         ...
    }

    if (...) {
        ...
   }

  if (...) {
       ... 
   }

  if (...) {
        ...
  }
 } 
}

is detected that one button was pressed, whole program should start from the beginning. I don't know really how to accomplish that.

http://arduino.cc/en/Reference/Break

is detected that one button was pressed, whole program should start from the beginning. I don't know really how to accomplish that.

What does "whole program should start from the beginning" mean? Re-run setup and start looping again? Just return to the beginning of loop for the next pass?

Assuming you have something like:

void setup() {
   // initialization code
}

void loop() {
  while (...) {
   ...
   if (counter == MAX) {
      counter = 0;
   }
 }

 for (i = 0; i < MAX; i++) {
    ...
 } 
}

If you want to detect the button press and exit loop() but continue to the next loop() call, just use the return keyword to exit loop():

void loop() {
  for (i = 0; i < MAX; ++i) {
    ...
    if (BUTTON_PRESSED) {
      return;
    }
    ...
  }
}

If you want to restart with setup() when a button press is detected, call it from within loop(), followed by a return:

void loop() {
  for (i = 0; i < MAX; ++i) {
    ...
    if (BUTTON_PRESSED) {
      setup()
      return;
    }
    ...
  }
}

If you want to restart with setup() when a button press is detected, call it from within loop(), followed by a return:

      setup()

Or not...

PaulS:

is detected that one button was pressed, whole program should start from the beginning. I don't know really how to accomplish that.

What does "whole program should start from the beginning" mean? Re-run setup and start looping again? Just return to the beginning of loop for the next pass?

I meant that it should start for the the beginning of void loop.

I haven't written it but after for (i = 0; i < MAX; i++) { and before every line until the end of that for loop Arduino should detect if one button was pressed and if it was contiune from the void loop { ...

void loop {
      ... 
      ... 

    for (i = 0; i < MAX; i++) {
       if (BUTTON_PRESSED != TRUE) {
              if (array[i] == 1) {
                 if (BUTTON_PRESSED != TRUE) {
                 digitalWrite(LED3, HIGH);
                 }
                 if (BUTTON_PRESSED != TRUE) {
                 digitalWrite(LED4, HIGH);
                 }
                 if (BUTTON_PRESSED != TRUE) {
                 delay(1000);
                 }
                 if (BUTTON_PRESSED != TRUE) {
                 digitalWrite(LED3, LOW);
                 }
                 if (BUTTON_PRESSED != TRUE) {
                 digitalWrite(LED4, LOW);
                 }
                if (BUTTON_PRESSED != TRUE) {
                 delay(1000);
                }
                if (BUTTON_PRESSED != TRUE) {
                }
                if (BUTTON_PRESSED != TRUE) {
                 Serial.print("Executing ");
                }
                if (BUTTON_PRESSED != TRUE) {
                 Serial.print(i);
                }
                 Serial.print(" ");
                 Serial.println(array[i]);
                 delay(500);
              }
              if (array[i] == 2) {
                 digitalWrite(LED1, HIGH);
                 digitalWrite (LED2, HIGH);
                 delay(1000);
                 digitalWrite(LED1, LOW);
                 digitalWrite(LED2,LOW);
                 delay(1000);
                 Serial.print("Izvrsavam ");
                 Serial.print(i);
                 Serial.print(" ");
                 Serial.println(array[i]);
                 delay(500);
              }
              if (array[i] == 3) {
                 digitalWrite(LED2, HIGH);
                 digitalWrite(LED4, HIGH);
                 delay(1000);
                 digitalWrite(LED2, LOW);
                 digitalWrite(LED4, LOW);
                 delay(1000);
                 Serial.print("Izvrsavam ");
                 Serial.print(i);
                 Serial.print(" ");
                 Serial.println(array[i]);
                 delay(500);
              }
              if (array[i] == 4) {
                   digitalWrite(LED1, HIGH);
                   digitalWrite(LED3, HIGH);
                   delay(1000);
                   digitalWrite(LED1, LOW);
                   digitalWrite(LED3, LOW);
                   Serial.print("Izvrsavam ");
                   Serial.print(i);
                   Serial.print(" ");
                   Serial.println(array[i]);
                   delay(500);
              }
       }
     }
}

Can you try:

             if (BUTTON_PRESSED != TRUE) {
                 digitalWrite(LED3, HIGH);
                 return;
             }