What does break; do in this function?

Hi all,

1st time poster here.

I am a member of a project where we have inherited code from a previous team. It contains a switch case, and for the life of me I can't wrap my head around it.

As far as I know, break; should send the code completely out of the switch.

void loop() {

int Sequence = 0;


case 0:
///here is a function
if (something){Sequence++}

case 1:
///// another function
if (something){Sequence++}

case 2:
///// a stop function


Now I would've thought that the break would've sent the code right out of switch, back to the start of the loop, and make sequence = 0 again, which would mean that case 1 and 2 don't happen?

Or does somehow, the sequence++ function mean that it will leap to case 1 or 2 without restarting?

I can't realistically post the whole code, because it's about 4 pages long, but this is the jist of it.

Any help would be appreciated!

Any help would be appreciated!

the Arduino reverence is helpful.


Hi BulldogLowell

I have read that, unfortunately, it does not answer my question.

Are you sure that

int Sequence = 0;

is not actually

static int Sequence = 0;

Using static would prevent the variable being reset to zero each time through loop()

UKHeliBob may have the answer to your problem. Loop() is actually a subroutine that is called by main() function (a function and call buried behind the Arduino IDE). So each time loop() is called, the value of Sequence is reset to 0. The Sequence++ instructions increment Sequence, but Sequence is reset on the next call of loop(). It is unlikely the switch{} can reach cases 1 or 2.

break; causes the program to 'exit' the switch{} function. If Sequence was declared as static, it would take 3 calls of loop() and switch{} to get from case 0 to case 2. Case 0, 1, and 2 will not execute in the same switch{} execution because of the break; instruction.

P.S. It is also good practice to include the default: case even if it is empty. The default case being what to execute if none of the above cases are met.

Ahhh, thanks Helibob, that seems to have solved it

and thanks to adwsystems for the clarification