Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript Loops.

Similar presentations


Presentation on theme: "JavaScript Loops."— Presentation transcript:

1 JavaScript Loops

2 Loops A loop is a block of code that allows you to repeat a section of code a certain number of times, perhaps changing certain variable values each time the code is executed.

3 Why loops are useful? Loops are useful because they allow you to repeat lines of code without retyping them or using cut and paste in your text editor. They save time and trouble of repeatedly typing the same lines of code, but also avoids typing errors in repeated lines. You are also able to change one or more variable values each time the browser passes through the loop.

4 For loop for ( varname=1;varname<11;varname+=1 ) initialization
Tells the loops when to stop running Determines the rate at which the variable is changed and whether it gets larger or smaller

5 While loop A while loop just looks at a short comparison and repeats until the comparison is no longer true. while(varname<11) { }

6 1. This is loop 1 2. This is loop 2 3. This is loop 3 4
1. This is loop 1 2. This is loop 2 3. This is loop 3 4. This is loop 4 5. Break the loop ! 6. This is loop 6 7. This is loop 7 8. This is loop 8 9. This is loop This is loop 10 Note: “IF”

7

8 Functions

9 User-defined function
Is a JavaScript code that is written to perform certain tasks repeatedly User-defined function Is one which the Web developer writes the tasks to be performed

10 Why functions are useful?
Helps organize various parts of a script into the difference tasks that must be accomplished Functions are portable. Functions are time-saving.

11 Defining the code for functions

12 Naming functions Function names are case sensitive
Function name must begin with a letter or an underscore character Function name cannot contain any spaces Avoid reserved words Give functions memorable and meaningful names

13 Calling functions in your scripts
A call to a function in JavaScript is simply the function name along with the set of parentheses, ending with a semicolon, like a normal JavaScript statement. functionname( )

14 <html> <head> </head> <body> <script type="text/javascript"> function display_alert() { alert("") } display_alert() </script> </body> </html>

15 <html> <head> <script type="text/javascript"> function myfunction() { alert(“This is an alert box"); } </script> </head> <body> <form> <input type="button" onclick="myfunction()" value="Call function"> </form> </body> </html>

16 Save your files here \\pshs-storage\CS3WEB\2nd Quarter\Magnesium_Functions\lastname_lastname.html


Download ppt "JavaScript Loops."

Similar presentations


Ads by Google