arduino Loops Flow Control

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

There are some ways to break or change a loop's flow.

break; will exit the current loop, and will not execute any more lines within that loop.

continue; will not execute any more code within the current iteration of the loop, but will remain in the loop.

The following loop will execute 101 times (i = 0, 1, ..., 100 ) instead of 1000, due to the break statement:

for (int i = 0; i < 1000; i++) {
    // execute this repeatedly with i = 0, 1, 2, ...
    if (i >= 100) {
        break;
    }
}

The following loop will result in j's value being 50 instead of 100, because of the continue statement:

int j=0;
for (int i = 0; i < 100; i++) {
    if (i % 2 == 0) { // if `i` is even
        continue;
    }
    j++;
}
// j has the value 50 now.


Got any arduino Question?