JavaScript Loops - PowerPoint PPT Presentation

About This Presentation
Title:

JavaScript Loops

Description:

A loop is a block of code that allows you to repeat a section of ... input type='button' onclick='myfunction()' value='Call function' /form /body /html ... – PowerPoint PPT presentation

Number of Views:869
Avg rating:3.0/5.0
Slides: 17
Provided by: jaysonb
Category:

less

Transcript and Presenter's Notes

Title: JavaScript Loops


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 ( varname1varnamelt11varname1 )
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(varnamelt11)

6
  • 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 9
  • 10. This is loop 10 Note IF

7
(No Transcript)
8
Functions
9
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
  • lthtmlgt
  • ltheadgt
  • lt/headgt
  • ltbodygt
  • ltscript type"text/javascript"gt
  • function display_alert()
  • alert("")
  • display_alert()
  • lt/scriptgt
  • lt/bodygt
  • lt/htmlgt

15
  • lthtmlgt
  • ltheadgt
  • ltscript type"text/javascript"gt
  • function myfunction()
  • alert(This is an alert box")
  • lt/scriptgt
  • lt/headgt
  • ltbodygt
  • ltformgt
  • ltinput type"button" onclick"myfunction()"
    value"Call function"gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

16
Save your files here \\pshs-storage\CS3WEB\2nd
Quarter\Magnesium_Functions\lastname_lastname.html
Write a Comment
User Comments (0)
About PowerShow.com