Can we put semicolon like while(condition);
in a C Programming? If while(condition);
is valid, what does it mean?
The while loop is used when we don't know the number of times it will repeat. If that number is infinite, or the Boolean condition of the loop never gets set to False, then it will run forever.
The while loop is used to repeat a section of code an unknown number of times until a specific condition is met. For example, say we want to know how many times a given number can be divided by 2 before it is less than or equal to 1.
The while loop loops through a block of code as long as a specified condition is true.
Contrast with the while loop, which tests the condition before the code within the block is executed, the do-while loop is an exit-condition loop. This means that the code must always be executed first and then the expression or test condition is evaluated. If it is true, the code executes the body of the loop again.
while (condition);
is the same as
while (condition) { }
It can be used for waiting loops, e.g.:
while (!isLightGreen()); // waits until isLightGreen() returns true go();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With