break

Description

break
is used to exit from a
for
,
while
or
do...while
loop, bypassing the normal loop condition. It is also used to exit from a
switch case
statement.

Example Code

In the following code, the control exits the

for
loop when the sensor value exceeds the threshold.

1int threshold = 40;
2 for (int x = 0; x < 255; x++) {
3 analogWrite(PWMpin, x);
4 sens = analogRead(sensorPin);
5 if (sens > threshold) { // bail out on sensor detect
6 x = 0;
7 break;
8 }
9 delay(50);
10 }

See also

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.