To program decision-making in JavaScript.
Web Programming book reading for THIS class
if
Statement: if
by Itselfif
Statement: else
and else if
Clausesswitch
Statementfor
LoopWeb Programming book reading for NEXT class
while
Loopdo
Loopfor
Loop (revisited)Sample Program
Additional Reading
var
and let
in JavaScript
(https://www.javascripttutorial.net/es6/difference-between-var-and-let)Questions from our last class or the readings or work between classes?
source: Connolly & Hoar, Fundamentals of Web Development, fig. 8.5, p. 360
Basic Concepts
Built-In Operator Precedence
source: Dean, Web Programming, p. 386, fig. 9.16
Boolean expressions
true
or
false
x > 10
LHS
operator RHS
means to compare the
left-hand side to the right-hand side using
the specified operator true
or
false
==
means equal to ===
means is precisely equal to>
means greater than,
for example, "Jesse" > "Heines"
(alphabetical order)<
means less than,
for example, x+2 < y
>=
means greater than or equal to,
for example, y >= 20
<=
means less than or equal to,
for example, z*10 <= y
&&
means and,
for example, a > 5 && a < 10
||
means or,
for example, b == 3 || b == 6
!
means not,
for example, ! c > 5
, which is equivalent to c <= 5
!=
means not equal to
&&
(and many other high-level languages) .and.
(notice the dots) AND(
BooleanExpression1,
BooleanExpression2 )
Making decisions using if
statements
if ( Boolean expression ) {
...
}
source: Dean, Web Programming, p. 362, fig. 9-5
if ( Boolean expression ) {
...
} else {
...
}
source: Dean, Web Programming, p. 368, fig. 9-9
if ( Boolean expression ) {
...
}
else if ( boolean expression ) {
...
}
else if ( boolean expression ) {
...
}
else {
...
}
source: Dean, Web Programming, p. 368, fig. 9-10
Making decisions using switch
statements
switch ( n ) { case 1 : // start here if n == 1 // first code block goes here break ; // stop here case 2 : // start here if n == 2 // second code block goes here break ; // stop here case 3, 4, 5 : // start here if n == 3 or 4 or 5 // third code block goes here break ; // stop here default : // start here if no other case matches (this clause is optional) // fourth code block goes here break ; // optional, but good practice }
Documenting code with //
or /*...*/
for
Loop
source: Connolly & Hoar, Fundamentals of Web Development, fig. 8.11, p. 373
k++
is the same ask = k + 1
- pre-increment means that the addition is done before
k
is used- post-increment means that the addition is done after
k
is usedfor ( var k = 0 ; k < 5 ; k++ ) { document.writeln( k + " " ) ; // output: 0 1 2 3 4 }
Preamble: I don’t expect you to finish this exercise today. Our next class will explore additional techniques that you can use to enhance this exercise, so we will continue working on it in that class.
Goal: To create a dynamic table.
Procedure:
JavaScript Multiplication Table (Heines)
0 1 2 3 4 5 6 7 8 9
0 10 20 30 40 50 60 70 80 90
<table>
and </table>
tags in standard HTML. for
loop to output 0-9 a single line of the desired
table. These numbers will be the first numbers to be multiplied.
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
for
loop to print the subsequent lines and
the results of the multiplication.
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 3 4 5 6 7 8 9 2 0 2 4 6 8 10 12 14 16 18
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 3 4 5 6 7 8 9 2 0 2 4 6 8 10 12 14 16 18
http://cssdweb.edu/username/filename.html
This is Class No. 14. It was last modified on
Saturday, October 21, 2023 at 4:38 PM. Copyright © 2010-2024 by Jesse M. Heines. All rights reserved, but may be freely copied or excerpted for
educational purposes with credit to the author.