Thursday, January 27, 2011

Looping in C

There are a few common loops that is used in C: while loop, do while loop and for loop.

while loop:

while (1)



The while loop above is an infinite loop.

for loop:

for (condition = 0; condition < set value; condition ++)



This loop is suppose to count up until it meet the condition meet the set value. To make a count down loop, set condition to a value, and as long as the condition > set value, condition -- .

Do while loop:



}while(condition != 0);

This loop will do at least once to check the condition is met or not. If the condition is not met, the program will exit. The while loop and for loop on the other hand, will check whether the condition is net before executing the program.

No comments:

Post a Comment