Knowledge Base
...
Applied PIC Micro Development
PIC Programming
"While" and "do While" Statements
9min
synopsis with the early advent of programming, it was very quickly realized that a way of repeating a set of instructions was required you could simply rerun the code over and over again, however for complex systems this would not be practical, especially where initialization of the system was required the while statement is c is similar to the goto statement in old basic syntax, however somewhat more elegant and useful this topic is introductory and should be read in conjunction with other topics in this series of training documents introduction looping through code is essential to any computerised system, where a set of instructions are repeatedly run and decisions are made based on those instructions to show a graphical representation of a loop, a simplified operation is shown in the diagram below the system remains active and does not shut down or reach an eol or end of line state and has nothing left to do the code is structured in such a way that the code is always run there are two types of while statements, the first is where the condition is checked before the code is run and the second is where the code is run and then the loop condition is checked there is a key difference here, and it is important to note as it does determine which of the two should be used while(condition) means that the code is run only if the condition is true do while(condition) means that the code is run regardless of whether the condition is true or not, so it runs at least once before it is stopped or not run again infinite loop to implement the infinite loop, you may use either of the two options below while(1){} and do {} while(1), provide the same result, ie the code will always loop even though while(1) checks the loop condition before hand and do while(1) checks the loop condition after the loop has run // first sample code while(1){ //code here } //and do { //code here } while(1); conditional loop if the code were to say while(a), this means while a = 1 or true then loop if the code were to say while(a==1), then this requires that the variable be initialized otherwise a would depend on the random nature of a pic at startup this can be precarious, as there is no way of knowing what the condition of the device will be if for example a = 0, then in that condition the following code would never run bit a; void main(){ 	 while(a==1){ …run commands; } } however, initialize a = 1 and the code runs for definite it would be a best practice to initialize the variable, especially for operation critical decisions bit a; void main(){ a=1; while(a==1){ …run commands; } } the above code is essentially repeating itself however, this setup can be used as an if statement in the code below, when the device is initialized a is equal to one or true however, during the course of the operation, the value of a can be changed to 0 the problem with the code below is that if it ever changes the back to 1, then the program essentially ends as the next command after a = 0 is nothing you would have to reboot the system to start again if that is the intention, then this is useful bit a; void main(){ a=1; while(a==1){ …run commands; } while(a==0){ …run commands; } } the standard way of writing code that loops is shown below an infinite loop is created, and within the loop, decisions are made concerning the outcome of each code path see below bit a; void main(){ a=1; while(1){ // loops indefinitely 	 if(a==1) { …run commands; } if(a==0) { …run commands; } } } the same would be true for the do while bit a; void main(){ a=1; do{ // loops indefinitely if(a==1) { …run commands; } if(a==0) { …run commands; } }while(1); } conclusion both statements have value depending on how or where they would be used at times this may come down to a programming style or preference it may also be based on when the condition is evaluated for the loop