Sunday, 1 February 2009

First 1500 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 inside the draw() method 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. In order to see what the code is doing, we will also use the print() and println() functions to display the location of each point by printing the coordinates to the console (the only difference between these 2 is that println() creates a new line). So, for example
println("point: " + 10);
results in the console output
[[[screenshot]]]
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 end point
while (currentPoint < width) {
// draw a single pixel point at that point, 20 pixels down
point(currentPoint, 20);
println("point:" + currentPoint);
// increment by 5 to leave a gap
currentPoint +=5;
}
// stop the draw method from executing again
noLoop();


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;
// if 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);
println("point:" + currentPoint);
// increment by 5 to leave a gap
currentPoint +=5;
}
// stop the draw method from executing again
noLoop();


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


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

point(currentPoint, 40);
println(" after");
}
// stop the draw method from executing again
noLoop();


In the above example, the loop iteration continues even though the value of currentPoint is no longer less-than width. The point is still drawn and the 'after' output is still printed to the console 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 as in the above examples, this need not (and often isn't) the case. While statements with counters tend not to look as neat as for statements. As an example, the first example using a for statement would look like:


// loop from 0 to the width of the window
for (int i = 0; i < width; i+=5) {
// draw a single pixel point at that point, 20 pixels down
point(i, 20);
println("point:" + i);

}
// stop the draw method from executing again
noLoop();


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


while (1 == 1) {
// create random coordinates
int xPoint = parseInt(random(0, width));
int yPoint = parseInt(random(0 , height));
// draw the point
point(xPoint, yPoint);
println(xPoint + " " + yPoint);
}


In the above example, the while statement loops forever because 1 is always equals to 1. If you run this, you will see the points being printed on the console, but you won't see the points on the screen because the draw method will never end. If you do run this, you can stop the program by closing the Processing window

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. This time we will create a 'target' square in the centre of the window, and draw random points until one of them lands in the target square. We will need to make sure that we know to exit the loop


// draw the target square
rect((width/2)-20, (height/2)-20, 40, 40);
boolean keepLooping = true;
// loop until told not to
while (keepLooping) {
// create random coordinates
int xPoint = parseInt(random(0, width));
int yPoint = parseInt(random(0 , height));
// draw the point
point(xPoint, yPoint);
println(xPoint + " " + yPoint);

// if the point is in the square
if (xPoint >= (width/2)-20 && xPoint <= (width/2)+20) {
if (yPoint >= (height/2)-20 && yPoint <= (height/2)+20) {
// stop looping
keepLooping = false;
}
}

}

noLoop();


In this example, we exit the while statement when a point lands in the square by checking the location of the point to see if it falls within the region the square occupies. If we hadn't written this so well, we could easily never set the keepLooping variable to false and therefore inadvertently created an infinite loop

There are various ways of using the condition to determine how many times the statement will execute. Any statement can be used as the condition provided it evaluates or returns a boolean value. This could be:

-a counter that is tested in the condition (like an if statement)
-a variable is tested for some criteria
-a boolean that is set it to false to exit
-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

Putting it all together

- big example


No comments: