Will this "break" statement work correctly.

The following is a part of my sketch. I have a timeout period running.

During that timeout period, if the XFER_IP arrives I need to delay for a time TORCH_OFF_DELAY then break out of the timeout loop.

I’m completely new to the break statement (and C programming in general) so not sure if I’ve done this correctly.
Sorry if anybody doesn’t like my bracketing method. I do it this way because I find it easy to see the structures of the functions.

while (MILLIS_NOW - TIMEOUT_START < TIMEOUT_PERIOD)
	{			                           
	  MILLIS_NOW = millis();			        
	   if(digitalRead(XFER_IP) == HIGH) 
	    {									   
	      delay (TORCH_OFF_DELAY);
	      break;                           
	    } 
	}

Edit:

Or if that coding is incorrect would this one work:

while (MILLIS_NOW - TIMEOUT_START < TIMEOUT_PERIOD) 
	{			                              
	  MILLIS_NOW = millis();
	   if(digitalRead(XFER_IP) == LOW)
	      continue;								   
	  delay (TORCH_OFF_DELAY);                           
	  break;                                              
	}

Keith.

And what happens?

Sorry Larry but I don't understand why you are asking that.

Whatever code I have after the "while" loop is executed once I have broken out of the loop, but that following code has no connection with the "while" loop.

Keith.

'break' will jump out of the innermost enclosing while, do-while, for or switch statement. Similarly 'continue' will jump to the end of the innermost enclosing while, do-while or for statement.

You want to break out of the while loop if the delay in the if statement gets executed? No it won't work the way you have it. Try setting MILLIS_NOW = TIMEOUT_START in the if statement. That will result in a condition that will exit the while loop. A flag could also be used. If the flag is not set stay in the while loop and set it in the if. This would be an or type of condition.

Thanks lads,

Mark
I’ve read what the break & continue statements do but my coding is slightly different to all the examples I’ve seen. For instance in my first example I have the delay statement between the “if” and the “break”, then in the second example I have the “break” on its’ own without the preceding “if”. All the examples I’ve looked at put the “if” before the “break” or the “continue”. Does break work when it’s all on its’ own without the preceding conditional if statement.

Jimmy,
I want to break out of the “while” loop when the XFER_IP becomes high, but from the moment that XFER_IP is high I need a delay (TORCH_OFF_DELAY) before I break out of the loop.
I am not trying to break out of the loop IF if the delay in the if statement gets executed. Wow, that sounds like a right mouthful :slight_smile:

Keith.

Would you show the complete sketch?

There is no requirement for a break to be enclosed in an if block. In both snippets you posted, when the break is executed, it will cause an exit from the while(), which is the innermost enclosing block of a type that the break acts upon.

lar3ry: There is no requirement for a break to be enclosed in an if block. In both snippets you posted, when the break is executed, it will cause an exit from the while(), which is the innermost enclosing block of a type that the break acts upon.

Wonderful, thanks very much Iar3ry. I like the way you have helped me understand how the break works.

The code compiled OK on the Arduino IDE but I didn't rely on that as meaning my code will work.

I've been busting my brains on this sketch for the last three days and that while loop has took forever to figure out. I only just read up on the "break" and "continue" statements this morning, never knew they existed before then.

Time to build the PCB and try the sketch.

Keith.