JavaScript - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

JavaScript

Description:

var Password = 'Sesame Street' var UserEntry. UserEntry = window.prompt('Please enter the password') if (UserEntry == Password) ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 7
Provided by: informat1027
Category:

less

Transcript and Presenter's Notes

Title: JavaScript


1
JavaScript
  • Conditional Repetition Statements

2
Conditional Statement
  • if..else statement
  • if (condition)
  • statement1
  • else
  • statement2

Multiple statements must be enclosed in braces
Example1
Example2
3
Example 1
  • ltSCRIPT LANGUAGE"JavaScript"gt
  • var RandNum Math.round(Math.random()100) 1
  • document.write(RandNum)
  • if (RandNum 2 0)
  • document.write(' is even.')
  • else
  • document.write(' is odd.')
  • lt/SCRIPTgt

The math object can be used to generate a random
number
4
Example 2
  • ltSCRIPT LANGUAGE"JavaScript"gt
  • var Password "Sesame Street"
  • var UserEntry
  • UserEntry window.prompt("Please enter the
    password")
  • if (UserEntry Password)
  • window.alert("That's right! Come on in!")
  • else
  • window.alert("Sorry! You didn't get it right!")
  • document.write("The password was ", Password)
  • lt/SCRIPTgt

5
For Loop
  • A for loop repeats a loop until a specified
    condition evaluates to false.
  • for (initial expression condition increment)
  • statements

Example
6
Example
  • function CountByOnes()
  • for (var x 1 xlt10 x)
  •    
  •     document.write(x)
  •                             

Note Curly braces start and end the function
and they also start and end the for loop.
7
While Loop
  • A while statement repeats a loop as long as a
    specified condition evaluates to true.
  • while(condition)
  • statements

Loop
Loop
Loop
Example1
Example2
8
Example1
  • ltSCRIPT LANGUAGE"JavaScript"gt
  • var count 1
  • while (count lt 10)
  • document.write(count, 'ltbrgt')
  • count
  • lt/SCRIPTgt

9
Example2
  • ltSCRIPT LANGUAGE"JavaScript"gt
  • var UserEntry
  • while (UserEntry ! "MFHS")
  • UserEntry window.prompt("Please enter 'MFHS'
    ")
  • document.write("Excellent! You entered 'MFHS' ")
  • lt/SCRIPTgt

10
Break Continue
  • The break statement terminates the current while
    or for loop and transfers program control to the
    statement following the terminated loop.
  • The continue statement terminates execution of
    the block of statements in a while or for loop,
    and continues execution of the loop with the next
    iteration.
  • In a while loop, it jumps back to the condition
  • In a for loop in jumps to the increment
    expression.

Example
Example
11
Example
  • var j 0
  • while (j lt 6)
  • if (j 3)
  • break
  • j
  • document.write(j)

The program will go to the statement following
the loop
Output 3
12
Example
  • var n 0, sum 0
  • while (n lt 6)
  • n
  • if (n 3)
  • continue
  • sumn
  • document.write(sum)

The program will go to the while statement,
increment n and continue.
Output 12
13
Infinite Loops
  • An infinite loop is a loop which never ends
    because there is no terminating condition.

ltSCRIPT LANGUAGE "JavaScript"gt var count
1 while (count lt10) document.write(count,
'ltBRgt') lt/SCRIPTgt
Write a Comment
User Comments (0)
About PowerShow.com