Infinite loops are a very bad practice in Embedded programming, One should never use an loop statement which runs forever unless it is the main system  loop. Programmers often use statements like


kind of statements which wait there until that condition is reached. If you use such statements and if something goes wrong and the condition you were waiting for never occurs, then your system will be hung there forever.

So if have to check such conditions then modify the statement to check only for a certain number of times and return with an error code if the maximum number of counts were reached,


unsigned short count = 0;

while( count < 65000)








What this loop does is it checks the condition only for 65000 times and breaks if the condition which we wanted is reached. You can always workaround and add any return codes/error codes if your want to indicate whether the break out of the loop was due to the count overflow or the flag was set. Later on you can handle these error codes as you want and take appropriate steps.

About the Author

is the author of this blog and is an Embedded systems design engineer by profession & Founder @ Valetron Systems Pvt Ltd

Email:This email address is being protected from spambots. You need JavaScript enabled to view it.

Find us on Google+



EasyEDA: Free Circuit Design Software

  • Easy Schematic Capture
  • Powerful Circuit Simulation
  • Online PCB design

Design Your Circuit and PCB here:

PCB Prototype Manufacturer - JLCPCB

Electronic Components Online Store