Sunday 7 December 2008

While Loops - First Draft



While blocks allow us to repeat a section of code, until a condition or conditions become true or false. The opening clause contains the condition and this is followed by a statement block. Statement blocks, in looping structures, are always enclosed by curly brackets, unless there is only one such statement. Sometimes a while statement is used to increment a simple counter. until it has a certain value.


while(condition/conditions){do this}//while condition is true do this
while(!condition){do this} //while not condition do this

int count=0;

while(count < face="courier new=">
callFunction(); //call function

count++;} //increase count's value by one


Here the loop is repeated 10 times. counts value is increased by one in each iteration of the while loop. It starts with value 0 and stops at value 10. Because count's value must be less than ten in the conditional clause, the statement block is not executed again and the code continues to the next statement, after the while block.

while(!input==valid){getInput();}

This will call the getInput() function, until the value of input is equal to the value of valid. (Here valid is probably a boolean variable with a value of true).
Examples of while loops in Chunk 16 are on lines: 42, 51 and 57.
The draw method has a lot of while methods that seem to be performing a similar function. The code looks like it could be optimised. We will try moving out this repetitious code to a function in a later section of Chunk 16.

No comments:

Post a Comment