using loops. goals understand how to create while loops in javascript. understand how to create...
Post on 21-Jan-2016
250 Views
Preview:
TRANSCRIPT
Using LoopsUsing Loops
GoalsGoalsUnderstand how to create while
loops in JavaScript.Understand how to create
do/while loops in JavaScript.Understand how to create simple
for loops in JavaScript.
Computing StructuresComputing StructuresRemember, last time we identified
that all executable statements fall into one of three categories:◦Sequential Structures – those
structures where instructions happen in sequence. That is “A before B”, “B before C”, “C before D”, etc.
◦Looping (Iterative) Structures – Today’s unit.
◦Decision (Selection) Structures – those structures where code alternate executes based on some Boolean test.
What is a Loop?What is a Loop?A loop is a programming structure that
contains code that will repeat until that code causes something to happen to satisfy or to not satisfy a given condition, thus ending the loop.
There are two basic “families” of loops:◦ Conditional Loops: Loops that depend solely on
some type of Boolean test.◦ Iterative Loops (a.k.a. For, For … Next loops): Loops
that depend on matching a maximum or minimum number of iterations (repetitions).
Parts of a LoopParts of a LoopAll loops share some basic parts.
These include:◦ Condition to test: This can be a Boolean test (for
conditional loops) or a test against a maximum or minimum integer value (for iterative loops).
◦ Executable block: The block of code that will execute so long as the loop has/hasn’t yet satisfied the condition tested.
◦ A way to end the loop: In terms of syntax, this is not necessary, but forgetting to include the method for ending the loop somewhere (usually the executable block) results in an endless loop.
Conditional LoopsConditional LoopsConditional loops are based on
some type of Boolean test.Conditional loops are useful when
the loop’s executable block should execute for an indeterminate length of time. You, as the programmer, don’t know how many times the executable block will execute in practice.
Conditional LoopsConditional LoopsConditional loops are often defined by
where the condition is written, in reference to the executable block:◦ Pre-test loops: The condition is located before the
executable block. There is a possibility that a pre-test loop may never execute. In JavaScript, the while loop is a type of pre-test loop.
◦ Post-test loops: The condition is located after the executable block. A pre-test loop’s executable block will always execute at least once. In JavaScript, the do … while loop is a type of post-test loop.
The The whilewhile Loop LoopAllows repeatable code so long as a
condition is evaluated to be TRUE.Terminates when something happens
that causes the condition to be evaluated as FALSE.
Useful when the program should go on for an indeterminate length of time (you, as the programmer, don’t know when the loop will end).
It is a pre-test loop; there is a chance it may never execute!
While Loop StructureWhile Loop Structure
while (condition to be tested){
executable block that will repeat if condition is TRUE
}
While Loop – Code While Loop – Code ExamplesExamples
Simple While – Example 1
Simple While – Example 2
While Loops and User While Loops and User ControlControlMany while loops have a definitive
ending specified in the condition programmed by the programmer (i.e. – counters).
However, sometimes we need code to repeat until the user (not the programmer) decides when it’s time to quit.
This can be done using a combination of while and alternative (if-then; if-then-else) structures.
Combining While & If Combining While & If StructuresStructures
Code Example
Data ValidationData ValidationWe can also use a while loop to
enforce validation of data the user enters.
Let’s say your creating the previous grade program and that you want the user to enter data that “fits” in the following ranges:◦ All user-entered data must be of number type (A)◦ All user-entered data must be less than 100,
inclusive (B)◦ All user-entered data must be greater than 0,
inclusive (C)
Data ValidationData ValidationWe already have one loop, to
take care of allowing the user to enter data until they enter “-1” (which we will keep).
Let’s add a nested loop to take care of the validation.
But how will we construct our condition? Remember that, we are actually testing 3 conditions …
Data ValidationData ValidationWe’ll created a compound condition,
using Boolean operators …Our 3 conditions:
◦ All user-entered data must be of number type (A)◦ All user-entered data must be less than 100,
inclusive (B)◦ All user-entered data must be greater than 0,
inclusive (C)How do we want to construct our
compound condition?◦ Do we want to force the user to re-enter if the user
fails all three tests?◦ Or do we want to force the user to re-enter if the
user fails any one of the tests?
Data ValidationData Validation
AA BB CC A&&BA&&B A&&BA&&B&&C&&C A||BA||B A||BA||B
||C||C
FF FF FF FF FF FF FF
FF FF TT FF FF FF TT
FF TT FF FF FF TT TT
FF TT TT FF FF TT TT
TT FF FF FF FF TT TT
TT FF TT FF FF TT TT
TT TT FF TT FF TT TT
TT TT TT TT TT TT TT
Testing for Numeric DataTesting for Numeric DataOkay, testing to see that data is
greater than 100 or less than 0 is easy, but how do we test for numeric data?
We can use a function called NaN()NaN() takes any type of data and tests
to see if that data can be converted to a numeric type – integer or float. If the data cannot be converted, the NaN() (NaN – “Not a Number”) returns a True value.
Data ValidationData Validation
Code Example
The The do whiledo while Loop LoopVery similar to a while loop:
◦Allows repeatable code so long as a condition is evaluated to be TRUE.
◦Terminates when something happens that causes the condition to be evaluated as FALSE.
◦Useful when the program should go on for an indeterminate length of time (you, as the programmer, don’t know when the loop will end).
It is a post-test loop; it will always execute at least once!
Do While Loop StructureDo While Loop Structure
do{
executable block that will repeat if condition is TRUE
}while (condition to be tested);
Do While Loop - ExampleDo While Loop - Example
Code Example
The For LoopThe For LoopThe for loop is an iterative loop.Repeats a code block until the loop
reaches a maximum or minimum value.
It is useful when the program should run on for a pre-determined length of time.
Derived from while loops with counters.
Parts of a For LoopParts of a For LoopThe loop conditions:
◦A starting number for the counter (usually 1 or 0)
◦A maximum or minimum value◦A way to increment/decrement the
counterThe executable block that will
execute so long as the maximum/minimum value is not yet met.
For Loop StructureFor Loop Structure
for(A; B; C){
executable block;}
KEY:
A – Counter’s starting value
B – Test again a maximum or minimum value
C – How to increment/decrement the counter.
KEY:
A – Counter’s starting value
B – Test again a maximum or minimum value
C – How to increment/decrement the counter.
For Loop ExampleFor Loop Example
Code Example
Questions?Questions?
top related