Download presentation
Presentation is loading. Please wait.
2
CHAPTER 4 DECISIONS & LOOPS
3
A script can do different things depending on what values it has been passed.
4
MAKING DECISIONS
5
Is test score greater than 50?
Y Message: Try again… Message: You passed!
6
if
7
if (score > 50)
8
Is test score greater than 50?
9
if (score > 50) { document.write(‘You passed!’); }
10
Is test score greater than 50?
Y
11
Is test score greater than 50?
Y Message: You passed!
12
if (score > 50) { document.write(‘You passed!’); } else { }
13
Is test score greater than 50?
Y Message: You passed!
14
if (score > 50) { document.write(‘You passed!’); } else { document.write(‘Try again…’); }
15
Is test score greater than 50?
Y Message: Try again… Message: You passed!
16
Is test score greater than 50?
Y Message: Try again… Message: You passed! CONTINUE SCRIPT…
17
COMPARISON OPERATORS
18
= = IS EQUAL TO
19
= = IS EQUAL TO ! = IS NOT EQUAL TO
20
= = IS EQUAL TO ! = IS NOT EQUAL TO = = = STRICT EQUAL TO
21
= = IS EQUAL TO ! = = = = ! = = IS NOT EQUAL TO STRICT EQUAL TO
STRICT NOT EQUAL TO
22
> GREATER THAN
23
> GREATER THAN < LESS THAN
24
> GREATER THAN < > =
LESS THAN > = GREATER THAN OR EQUAL TO
25
> GREATER THAN < > = < =
LESS THAN > = GREATER THAN OR EQUAL TO < = LESS THAN OR EQUAL TO
26
LOGICAL OPERATORS
27
if (score > 75)&&(score < 95) { document.write(‘Very good!’); }
28
if (score > 75)&&(score < 95) { document.write(‘Very good!’); }
29
& & LOGICAL AND
30
& & LOGICAL AND | | LOGICAL OR
31
& & LOGICAL AND | | LOGICAL OR ! LOGICAL NOT
32
SWITCH STATEMENTS
33
switch
34
switch (level) { }
35
switch (level) { case ‘One’: title = ‘Level 1’; break; }
36
switch (level) { case ‘One’: title = ‘Level 1’; break; case ‘Two’: title = ‘Level 2’; break; }
37
switch (level) { case ‘One’: title = ‘Level 1’; break; case ‘Two’: title = ‘Level 2’; break; case ‘Three’: title = ‘Level 3’; break; }
38
switch (level) { case ‘One’: title = ‘Level 1’; break; case ‘Two’: title = ‘Level 2’; break; case ‘Three’: title = ‘Level 3’; break; default: title = ‘Test’; break; }
39
LOOPS
40
for (var i = 0; i < 3; i++) { document.write(i); }
41
for (var i = 0; i < 3; i++) { document.write(i); }
KEYWORD
42
for (var i = 0; i < 3; i++) { document.write(i); }
CONDITION (COUNTER)
43
for (var i = 0; i < 3; i++) { document.write(i); }
The variable i is declared and set a value of 0 INITIALIZATION
44
for (var i = 0; i < 3; i++) { document.write(i); }
Every time the loop is run, the condition is checked to see if i is less than 3 CONDITION
45
for (var i = 0; i < 3; i++) { document.write(i); }
If i is less than 3, the code block is run
46
for (var i = 0; i < 3; i++) { document.write(i); }
The variable i can be used inside the loop (here it is used to write its value to the page)
47
for (var i = 0; i < 3; i++) { document.write(i); }
When the code inside the curly braces has been executed, the variable i is incremented by 1 UPDATE
48
ANATOMY OF A LOOP
49
2 1 i = 0 i = 1 i = 2 i = 3
50
i = 0
51
Is i less than 3? i = 0
52
Yes, i is less than 3 i = 0
53
Write i to the page i = 0
54
Add 1 to i i = 0 i = 1
55
i = 0 i = 1
56
i = 0 i = 1 Is i less than 3?
57
i = 0 i = 1 Yes, i is less than 3
58
i = 0 i = 1 Write i to the page 1
59
i = 0 i = 1 Add 1 to i i = 2 1
60
1 i = 0 i = 1 i = 2
61
1 i = 0 i = 1 i = 2 Is i less than 3?
62
1 i = 0 i = 1 i = 2 Yes, i is less than 3
63
1 i = 0 i = 1 i = 2 Write i to the page 2
64
1 i = 0 i = 1 i = 2 Add 1 to i i = 3 2
65
2 1 i = 0 i = 1 i = 2 i = 3
66
2 1 i = 0 i = 1 i = 2 i = 3 Is i less than 3?
67
2 1 i = 0 i = 1 i = 2 i = 3 No, i is not less than 3
68
2 1 i = 0 i = 1 i = 2 i = 3 End loop
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.