Tuesday, 27 January 2009

First 1000 words (draft)

While Statements

The while statement, like a for statement, repeats a number of times executing the code inside the statement each time. However, whilst for statements repeat for a set number of times (iterations), while statements repeat as long as the boolean condition is true – the loops exits when the condition is false. In this respect, a while statement is like an if statement that is repeated until the condition is no longer true

To demonstrate while statements in Processing, we will use the point() function to create dots on the screen. The point() function simply draws a one pixel point at the coordinates given as arguments to the function in the form
point(x, y);

where x is the number of pixels along the horizontal axis and y is the number of pixels down the vertical axis. So, for example, the function call
point(10, 20);

will draw a point at the coordinate (10, 20) which is 10 pixels from the left of the window, and 20 pixels from the top

A simple example of a while statement


// set the current point as the start point
int currentPoint = 0;
// while the current point is less
//than the width of the window
while (currentPoint < width) {
// draw a single pixel point at that
//point, 20 pixels down
point(currentPoint, 20);
// increment by 2 to leave a gap
currentPoint +=2;
}


This will produce the output below – a screen with a lot of dots across it.

screenshot

If we converted this into an if statement, rather than a while statement, you will be able to see the difference


// set the current point as the start point
int currentPoint = 0;
// while the current point is less
// than the end point
if (currentPoint < width) {
// draw a single pixel point at that
// point, 20 pixels down
point(currentPoint, 20);
// increment by 2 to leave a gap
currentPoint +=2;
}


Although it is quite hard to see, in the screenshot below you should be able to see a single dot on the left. The if statement has only created one dot, whereas the while statement kept creating dots until it's condition (currentPoint < width) was false i.e. when currentPoint reaches the width of the window

screenshot

Exit condition

The loop will exit at the point that the condition is checked and found to be false, not at the point that the condition becomes false. Therefore the remainder of the loop iteration will complete and the loop will exit when the condition is evaluated

example

In the above example, the loop continues even though the value of [whatever the condition is]. The [whatever the line is] is still [run/output?] because that iteration still completes before the while statement exits when the condition is re-evaluated.

Compare to for loop

You may be wondering at this point what the point of a for statement is if a while statement can do the same thing. If they seem similar it is because they are – a for statement provides a simple way of using a counter to define the number of times the statement loops. This means that the number of times the statement runs is known. However, the difference lies in how you know when to exit the loop. Although a while statement can use a counter, this need not (and often isn't) the case. While statements with counters tend not to look as neat as for statements.

compare for and while examples

The condition for exiting a while statement is not usually as straightforward – it is likely that you will not know exactly how many times it will loop when you write it as the value of the condition will be determined while the program is running. It is also important to remember that it is possible to write a while statement that will loop forever – an infinite loop – because the condition never becomes false

-- deliberate infinite loop example (tell them how to exit it)

It is therefore necessary to have a well-defined condition that you know will exit at some point. A badly designed condition can result in an infinite loop that you didn't intend

-- give examples – one good one bad (checking a number that never occurs using ==)

There are various ways of using the condition to determine how many times the statement will execute

set a counter and test in the condition (like an if statement)
test a variable for some criteria
set a boolean and set it to false to exit
call a method that returns a boolean



while using arrays
- simple array example
- using a boolean
(possibly using the result of a method call)


do.. while

Just as it is possible to have a while statement that loops forever, it is also possible to have one that never executes if the condition is never met. However, sometimes you might always want a section of code to be executed even if the condition is not met. An example of this is [think of an example]
Using a while statement, the code to be executed would have to be repeated – at the very least a method would have to be called twice (example?)

In this situation, there is another type of loop statement that can be used – a do..while statement. A do..while statement is like a while loop but evaluates the condition after the body of the loop has been executed. This means that the code inside the body is always executed at least once – if the condition is found to be false, the loop will exit at that point having already executed the code

example



- using arrays

It can be dangerous to always execute the code inside the loop statement if you do not know what the effect of that would be. [explain]. If there is a chance that the code should never be executed, then a while statement would be more appropriate. Do .. while statements should only be used when you know that need the loop code to be executed as least once. As such, they are seldom used, but do provide a useful way of eliminating duplicate code

big example

No comments: