JavaScript V - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

JavaScript V

Description:

execute code only if test is true. we can think of this as one step. not ... if affiliation is Democrat. Democrat primary ballot. non-partisan referendum ballot ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 23
Provided by: rbu
Category:

less

Transcript and Presenter's Notes

Title: JavaScript V


1
JavaScript V
  • Conditional Execution

2
Outline
  • Conditional statement
  • If-then
  • If-then-else
  • Nested If
  • Boolean expressions

3
Program execution
  • Unconditional execution
  • interpreter executes each line in sequence
  • Conditional execution
  • evaluate a test
  • execute code only if test is true
  • we can think of this as one step
  • not always executed
  • Need not be a single step
  • might be a chunk of code controlled by the
    condition
  • call the "body"

program step 1 program step 2 program step
3 program step 4 program step 5
condition conditional step program step
2 program step 3 program step 4
4
Examples
  • if due date on book is before today's date
  • charge overdue fee
  • if day of the week is not sunday or holiday
  • parking meter must be used
  • if even day of the month and date between Nov 1
    and April 1
  • car will be ticketed and towed

5
JavaScript
  • if statement
  • if (condition) statement or
  • if (condition) statements
  • Example
  • if (dateToday gt dueDate)
  • bookOverdue true
  • or
  • if (dateToday gt dueDate)
  • bookOverdue true
  • calculateFine (dateToday, dueDate)
  • ...

6
If-then-else
  • Conditional execution
  • evaluate a test
  • execute one piece of code if test is true
  • execute another piece if test is false
  • we can think of this as one step
  • one part or the other will be executed

If (condition) conditional true step(s) else
condition false step(s) program step 2 program
step 3 program step 4
program step 1
7
Examples
  • if the title is a new release
  • then charge 3.00 and lending period is 2 days
  • else charge 2.00 and lending period is 5 days
  • if the answer is correct
  • then add 1 to score
  • else print help message

8
JavaScript
  • if statement
  • if (condition)
  • then part
  • else
  • else part
  • Example
  • if (newRelease(title))
  • charge 3.00
  • lendingPeriod 2
  • else
  • charge 2.00
  • lendingPeriod 5

9
Stylistic issues
  • Where to put brackets
  • I prefer after condition
  • if (condition)
  • ... body ...
  • else
  • ... body ...
  • Some prefer on separate lines
  • if (condition)
  • ... body ...
  • else
  • ... body ...
  • In some cases, you might put whole statement on
    one line
  • if (condition) ... body ...

10
Example
  • ifdemo.html
  • ifdemo2.html (with else)
  • chill.html

11
Nested If
  • There may be more than two possibilities to be
    handled
  • Put one if statement inside another
  • if (temp lt 50)
  • if (windSpeed lt 3)
  • windChill temperature
  • else
  • windChill ...calculation here...
  • else
  • ... wind chill undefined ...
  • Example chill2.html

12
Exercise
  • if (x gt y)
  • if (x10 lt 100)
  • document.write(ONE)
  • else
  • document.write(TWO)
  • else
  • document.write(THREE)

x 0 y 5 output ? ? x 0 y
-5 output ? ? x 9 y 9 output ? ? x
22 y 21 output ? ?
13
How to handle
  • Lots of possibilities
  • if affiliation is Republican
  • Republican primary ballot
  • non-partisan referendum ballot
  • if affiliation is Democrat
  • Democrat primary ballot
  • non-partisan referendum ballot
  • if affiliation is Green
  • Green Party primary ballot
  • non-partisan referendum ballot
  • otherwise
  • non-partisan referendum ballot
  • Nesting gets ugly

14
Cascading ifs
  • Easier to read than nested style
  • It is clear that multiple alternatives are
    involved
  • if (condition1) then
  • action1
  • else if (condition2) then
  • action2
  • else if (condition3) then
  • action3
  • etc
  • Some languages (perl) actually have a special
    elsif construct

15
Examples
  • dice.html
  • Reed style
  • twodice.html
  • note image list access
  • with counter
  • In-class Exercise with double counter?
  • Exercise (on your own)
  • Rewrite wind chill code as cascading ifs

16
Equality operators
  • Equal
  • Not equal !
  • Inequality gt, gt, lt, lt
  • These are operators just like , -, etc.
  • What data type do they return?
  • i lt 5

17
Decision Making Equality and Relational
Operators
18
If Statement Gotcha 1Type Sensitivity
  • The result of an inequality depend on data type
  • 100 gt 18
  • "100" lt "18"

19
If Statement Gotcha 2Equality vs assignment
  • Equality test (d 0)
  • Boolean value
  • Assignment (d 0)
  • the assignment statement also has a value
  • whatever the new value is
  • bad language design!
  • Unexpected behavior
  • if (done false)
  • .. do something ..
  • never executed
  • Worse yet
  • "", 0 are automatically converted to false
  • if (p 5)
  • .. do something ..
  • always executed
  • Check that if statements only use the equality
    operator

20
Boolean Operators
  • and
  • or
  • !
  • not

21
Boolean expressions
  • Expressions returning Boolean values
  • can be used as a condition in an if statement
  • if wind speed above 3 and temperature below 50
  • calculate wind chill
  • if (windSpeed gt 3 temperature lt 50)
  • calculate (windSpeed, temperature)

22
Other Examples
  • die1 RollDie()
  • die2 RollDie()
  • doubles?
  • total is 7?
  • roll is a 1 and 1?
  • roll contains a 3?
  • total is 7 or 11?
Write a Comment
User Comments (0)
About PowerShow.com