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
Subscribe to:
Posts (Atom)