return; inside the loop()?

I try to learn by deconstructing sample code, but I've never seen this. A return inside of the loop.

//*********************************** Loop ***********************************
void loop() {

  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

What does it return to?
I am just guessing, but does this skip all the code that follows to the end of the loop?

It returns to the hidden main() function, which will just call loop() again. Yes, it has the effect of skipping everything below that point.

It returns to the function that called it:

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

(see ArduinoCore-avr/main.cpp at master · arduino/ArduinoCore-avr · GitHub )

The for(;:wink: is an infinite loop that calls loop() and then checks for a serial event.

Returning to this is what the happens when loop() hits the end brace anyway; there's no harm in returning earlier.

Thank you both for the quick reply.
Is this an acceptable structure, or should I be nesting if statements? I kind of like the idea of skipping to the end if the test fails.

And, Blackfin, thanks. I had never seen the parent code for the sketch.

Note, you can also effectively use ‘return’ to leave ‘any’ function early based on conditions.

SteveMann:
Thank you both for the quick reply.
Is this an acceptable structure, or should I be nesting if statements?

It is if it makes your code easier to understand. I often use premature returns to avoid a deeply nested IF structure that would be hard to understand.

Make sure to first do the test that is most likely to fail as that shortens the time for the code in the greatest number of cases.

...R