using loops. goals understand how to create while loops in javascript. understand how to create...

26
Using Loops Using Loops

Upload: daniela-pope

Post on 21-Jan-2016

250 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

Using LoopsUsing Loops

Page 2: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 3: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 4: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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).

Page 5: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 6: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 7: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 8: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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!

Page 9: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

While Loop StructureWhile Loop Structure

while (condition to be tested){

executable block that will repeat if condition is TRUE

}

Page 10: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

While Loop – Code While Loop – Code ExamplesExamples

Simple While – Example 1

Simple While – Example 2

Page 11: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 12: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

Combining While & If Combining While & If StructuresStructures

Code Example

Page 13: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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)

Page 14: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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 …

Page 15: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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?

Page 16: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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

Page 17: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 18: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

Data ValidationData Validation

Code Example

Page 19: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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!

Page 20: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

Do While Loop StructureDo While Loop Structure

do{

executable block that will repeat if condition is TRUE

}while (condition to be tested);

Page 21: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

Do While Loop - ExampleDo While Loop - Example

Code Example

Page 22: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 23: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 24: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

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.

Page 25: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

For Loop ExampleFor Loop Example

Code Example

Page 26: Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create

Questions?Questions?