Professional Documents
Culture Documents
Lecture 23
Flow Control & Loops
(Web Development Lecture 8)
1
During the last lecture we had a discussion
on Data Types, Variables & Operators
• We found out about JavaScript data types
2
JavaScript Data Types
• JavaScript recognizes & distinguishes among
the following types of values:
– Numbers
– Booleans
– Strings
– Undefined
3
Variables
• Variables give us the ability to manipulate data
through reference instead of actual value
4
Declaring Variables
Although JavaScript allows variable
declaration, it does not require it - except in
the case when we want to declare a variable
being local (more on local variables later in
the course!)
5
JavaScript Variables are Dynamically Typed
6
JavaScript Operators
JavaScript has numerous operators, classified
in many categories. We will look at only a few
of them belonging to the following categories:
– Assignment operators
– Arithmetic operators
– Comparison operators
– Logical operators
– String operators
7
comments let
the code speak
for itself!
8
Decimal to Binary Conversion in JavaScript
x = 75 ; // x is the decimal number
y = “” ; // y is the binary equivalent
while ( x > 0 ) {
remainder = x % 2 ;
quotient = Math.floor( x / 2 ) ;
y = remainder + y ;
x = quotient ;
}
document.write( “y = ” + y ) ; 9
Today’s Lecture:
Flow Control & Loops
• We’ll try to understand the concept of flow
control using the “if” and “switch” structures
True False
condition
statement statement
block 1 block 2
12
JavaScript Flow Control Structures
if … else
switch
13
if: Example 1
if ( day == “Sunday” )
bhola = “Cool” ;
The condition
enclosed in
parentheses semicolon
Set the value of the variable
‘bhola to ‘Cool’ if the ‘day’ is
equal to ‘Sunday’
14
This was the case if we want to
execute a single statement given that
the condition is true
15
if: Example 2
if ( day == “Sunday” ) {
bhola = “Cool” ;
mood = “Great” ;
clothing = “Casual” ;
} These curly braces
group the multiple
statements into a
single compound
Set the value of the variable ‘bhola to ‘Cool’, ‘mood’
statement
to ‘Great’, and ‘clothing’ to ‘casual’ if the ‘day’ is
equal to ‘Sunday’
16
if: Example 2
if ( day == “Sunday” ) {
bhola = “Cool” ;
mood = “Great” ;
clothing = “Casual” ;
}
Note: No semicolon
after the closing
curly brace
Set the value of the variable ‘status’ to ‘Cool’, ‘mood’
to ‘Great’, and ‘clothing’ to ‘casual’ if the ‘day’ is
equal to ‘Sunday’
17
Compound Statements
1. At times, we need to put multiple statements
at places where JavaScript expects only one
18
Compound Statements
3. This is done simply by enclosing any number
of statements within curly braces, { }
19
if: Example 3
if ( (day == “Sunday”) || (day == “Saturday”) ) {
bhola = “Cool” ;
mood = “Great” ;
clothing = “Casual” ;
}
20
if: Example 4
22
if … else: Example 1
23
if … else: Example 2
24
if … else: Example 3
25
if … else: Example 4
26
if … else: Example 5
if ( grade == “A” )
points = 4.0 ; This piece of
if ( grade == “B” ) code is
points = 3.0 ; correct, but
if ( grade == “C” ) not very
points = 2.0 ; efficient!
if ( grade == “D” )
What can we
points = 1.0 ;
do to
if ( grade == “F” )
improve it?
points = 0.0 ;
27
Nested
if … else
Structures
28
if ( grade == “A” )
points = 4.0 ;
if … else:
else { Example 6
if ( grade == “B” )
points = 3.0 ;
else {
if ( grade == “C” )
points = 2.0 ;
else {
if ( grade == “D” )
points = 1.0 ;
else
points = 0.0 ;
}
}
29
}
JavaScript Flow Control Structures
if … else
switch
30
switch ( grade ) {
A colon switch:
case “A” :
points = 4.0 ; following Example 1
break ; the case
case “B” : label is
The expression
points = 3.0 ; required
enclosed in
break ; parentheses is
case “C” : evaluated and
points = 2.0 ; This is a
case label matched with
break ; case labels
case “D” :
points = 1.0 ; This ‘break’
break ; statement is the
default : exit The
point‘default’ statement
points = 0.0 ; acts like the ‘else’31clause
} in the ‘if…else’ structure
switch ( inquiry ) {
switch: Example 2
case “apple” :
document.write( “Apples are Rs 50/kg” ) ;
break ;
case “mangos” :
document.write( “Mangos are Rs 90/kg” ) ;
break ;
case “grapes” :
document.write( “Grapes are Rs 60/kg” ) ;
break ;
default :
document.write( inquiry + “? Please retry!” ) ;
} 32
?
if … else
switch
33
if…else --?-- switch
• If the action to be taken of the value of a
single variable (or a single expression),
use ‘switch’
34
if … else: Example 7
35
Loops
36
Loop through a set of statements
as long as a condition is true
statement True
condition
block
False
37
JavaScript’s Looping Structures
while
for
38
Decimal to Binary Conversion in JavaScript
The condition
x = 75 ; // x is the decimal number
enclosed in
y = “” ; // y is the binary equivalent
parentheses
while ( x > 0 ) {
remainder = x % 2 ;
quotient = Math.floor( x / 2 ) ;
y = remainder + y ;
x = quotient ;
}
document.write( “y = ” + y ) ; 39
while: Example 2
40
while: Example 3
x=1;
while ( x < 6000 ) {
document.write ( x ) ;
x=x+1;
}
41
JavaScript’s Looping Structures
while
for
42
for: Example 1
x=1;
Initial count
while ( x < 6000 ) {Condition Operation
document.write ( x ) ;
x=x+1;
} for ( x = 1 ; x < 6000 ; x = x + 1 ) {
document.write ( x ) ;
}
43
for: Description (1)
1. The ‘for’ loop starts by initializing the counter
variable (which in this case is x)
44
for: Description (2)
4. After reaching the end of that iteration, the ‘for’
loop goes to the top once again, performs the
operation, checks the condition
46
for: Example 3
6000? 47
for: Example 4
None? 48
?
for
while
49
for --?-- while
• When the exact number of iterations is
known, use the ‘for’ loop
50
‘for’ loops become especially useful
when used in conjunction with arrays
51
During Today’s Lecture …
• We discussed the concept of flow control using
the “if” and “switch” structures
52
Next (the 9th) Web Dev Lecture:
Arrays
53