Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create."— Presentation transcript:

1 Using Loops

2 Goals Understand 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.

3 Computing Structures Remember, 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.

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

5 Parts of a Loop All 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.

6 Conditional Loops Conditional 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.

7 Conditional Loops Conditional 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.

8 The 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 pre-test loop; there is a chance it may never execute!

9 While Loop Structure while (condition to be tested) { executable block that will repeat if condition is TRUE }

10 While Loop – Code Examples Simple While – Example 1 Simple While – Example 2

11 While Loops and User Control Many 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.

12 Combining While & If Structures Code Example

13 Data Validation We 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)

14 Data Validation We 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 …

15 Data Validation We’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?

16 Data Validation ABCA&&B A&&B &&C A||B A||B ||C FFFFFFF FFTFFFT FTFFFTT FTTFFTT TFFFFTT TFTFFTT TTFTFTT TTTTTTT

17 Testing for Numeric Data Okay, 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.

18 Data Validation Code Example

19 The do while Loop Very 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!

20 Do While Loop Structure do { executable block that will repeat if condition is TRUE } while (condition to be tested);

21 Do While Loop - Example Code Example

22 The For Loop The 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.

23 Parts of a For Loop The loop conditions: ◦ A starting number for the counter (usually 1 or 0) ◦ A maximum or minimum value ◦ A way to increment/decrement the counter The executable block that will execute so long as the maximum/minimum value is not yet met.

24 For 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/decremen t the counter. KEY: A – Counter’s starting value B – Test again a maximum or minimum value C – How to increment/decremen t the counter.

25 For Loop Example Code Example

26 Questions?


Download ppt "Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create."

Similar presentations


Ads by Google