Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the code "while(condition);" valid and what does it mean?

Tags:

c

while-loop

Can we put semicolon like while(condition); in a C Programming? If while(condition); is valid, what does it mean?

like image 411
user500817 Avatar asked Nov 08 '10 15:11

user500817


People also ask

What is the condition in a while loop?

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.

What is the purpose of the while () method?

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.

Does a while loop need a condition?

The while loop loops through a block of code as long as a specified condition is true.

Is do-while a valid loop?

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.


1 Answers

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(); 
like image 61
Curd Avatar answered Sep 18 '22 17:09

Curd