When a “while” loop starts execution, the loop condition is first checked. If it happens to be true, the following statements (the Loop body) are executed and repeated until the condition becomes false. Checking the condition before entering the loop is called a pre-check.
When a while loop begins execution, the computer first checks the loop’s condition. If the condition is true, the statements included in the loop are executed and repeated until the condition becomes false. Checking the condition before entering the loop is called a pre-check.
A do-while loop is another variant of the while loop where the condition is appended at the end of the loop so that the loop is executed at least once before the condition is checked. Do-while loops are used when we want the body of the loop to be executed once because a certain variable must be updated before we run the checking condition. If the while condition is true when the loop has been run once, the body is re-executed until the condition becomes false. This is called post-check.
Sometimes we accidentally program what is called by an infinite loop , which occurs when a loop's condition will never be changed to false so that the loop can end. A famous example of this behavior is the while(1) loop, which will run forever. Loop like that are typically used in embedded systems that run the same firmware over and over again. For loops can be used to add delays to some systems by specifying the clock cycle speed of the processor and then seeing how many loop cycles will be equal to the time we need.