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, 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
explain while loop
simple example
if statement example
Compare to for loop
- for loops are a coding shortcut for a while loop with a counter
- examples
- loop forever until exit condition
- exit condition needs to be known in advance
explain exactly when the loop exits
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.
- example
You may be wondering at this point what the point of a for statement is if a while statement can do the same thing. It is true that anything that can be done in a while statement can also be done in a for statement. However, the difference lies in how you know when to exit the loop – 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
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. It is also important to remember that it is possible to write a while statement that will loop forever – an infinite loop
-- 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)
set a variable and test it 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
- using arrays
- danger of always doing one iteration
big example
Wednesday, 10 December 2008
Friday, 14 November 2008
Vague outline
While Loops
explain while loop
- simple example
Compare to for loop
- for loops are a coding shortcut for a while loop with a counter
- examples
- loop forever until exit condition
- exit condition needs to be known in advance
- explain exactly when the loop exits
while using arrays
- simple array example
- using a boolean
- (possibly using the result of a method call)
do.. while
- explain
- difference to while loop
- using arrays
- danger of always doing one iteration
big example
explain while loop
- simple example
Compare to for loop
- for loops are a coding shortcut for a while loop with a counter
- examples
- loop forever until exit condition
- exit condition needs to be known in advance
- explain exactly when the loop exits
while using arrays
- simple array example
- using a boolean
- (possibly using the result of a method call)
do.. while
- explain
- difference to while loop
- using arrays
- danger of always doing one iteration
big example
Sunday, 9 November 2008
Chunk 13 Description
CHUNK 13
TITLE While statements
DESCRIPTION Describe the two forms of while statement. Students really find this a tough topic so go very gently with lots of examples. Use arrays as much as possible but start of with a simple non-array example such as summing the first n integers. Develop a program similar to Greenberg 90--92
OUTCOMES
HINT This probably the toughest part of the book so far, take it very gently with lots of small examples between three and five lines of code
PROGRAM Develop a program similar to the one you describe in the text.
TITLE While statements
DESCRIPTION Describe the two forms of while statement. Students really find this a tough topic so go very gently with lots of examples. Use arrays as much as possible but start of with a simple non-array example such as summing the first n integers. Develop a program similar to Greenberg 90--92
OUTCOMES
- Be able to understand a simple while loop involving a single dimensional array
- Be able to develop a simple while loop involving a single dimensional array
- Be able to understand a program of a similar complexity to that described in your text
HINT This probably the toughest part of the book so far, take it very gently with lots of small examples between three and five lines of code
PROGRAM Develop a program similar to the one you describe in the text.
Monday, 3 November 2008
Book arrived and chunk order
The processing book finally arrived so I can finally start playing with the IDE. As I'm doing 2 chunks (13 and 82) I've decided to do chunk 82 first, as it looks like fun. The chunk 82 blog is here:
http://masswritingchunk82.blogspot.com/
http://masswritingchunk82.blogspot.com/
Wednesday, 29 October 2008
Setting Up
This blog is for chunk 13 (While statements) of the Open University Mass Writing project. More to come soon
Subscribe to:
Posts (Atom)