Title: CS101 Introduction to Computing Lecture 23 Flow Control
1CS101 Introduction to ComputingLecture 23Flow
Control Loops (Web Development Lecture 8)
2During the last lecture we had a discussion on
Data Types, Variables Operators
- We found out about JavaScript data types
- About variables and literals
- We also discussed several operators supported by
JavaScript
3JavaScript Data Types
- JavaScript recognizes distinguishes among the
following types of values - Numbers
- Booleans
- Strings
- Undefined
4Variables
- Variables give us the ability to manipulate data
through reference instead of actual value - Variables are containers that hold values
5Declaring 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!)
6JavaScript Variables are Dynamically Typed
- Any variable in JavaScript can hold any type of
value, and the that type can change midway
through the program
7JavaScript 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
8comments let the code speak for itself!
9Decimal to Binary Conversion in JavaScript
- x 75 // x is the decimal number
- y // y is the binary equivalent
- while ( x gt 0 )
- remainder x 2
- quotient Math.floor( x / 2 )
- y remainder y
- x quotient
-
- document.write( y y )
10Todays LectureFlow Control Loops
- Well try to understand the concept of flow
control using the if and switch structures - And also the concept behind the while and for
looping structures - We will solve simple problems using flow control
and loop structures
11Flow Control
12- Select between alternate courses of action
depending upon the evaluation of a condition
condition
True
False
statement block 1
statement block 2
13JavaScript Flow Control Structures
14if Example 1
- if ( day Sunday )
- bhola Cool
- Set the value of the variable bhola to Cool if
the day is equal to Sunday
The condition enclosed in parentheses
semicolon
15This was the case if we want to execute a single
statement given that the condition is trueWhat
if we want to execute multiple statements in case
the condition is true?
16if Example 2
- if ( day Sunday )
- bhola Cool
- mood Great
- clothing Casual
-
- Set the value of the variable bhola to Cool,
mood to Great, and clothing to casual if
the day is equal to Sunday
These curly braces group the multiple statements
into a single compound statement
17if Example 2
- if ( day Sunday )
- bhola Cool
- mood Great
- clothing Casual
-
- Set the value of the variable status to Cool,
mood to Great, and clothing to casual if
the day is equal to Sunday
Note No semicolon after the closing curly brace
18Compound Statements
- At times, we need to put multiple statements at
places where JavaScript expects only one - For those situations, JavaScript provides a way
of grouping a number of statements into a single
statement, called a statement block
19Compound Statements
- This is done simply by enclosing any number of
statements within curly braces, - NOTE Although the statements within the block
end in semicolons, the block itself doesnt
20if Example 3
- if ( (day Sunday) (day Saturday) )
- bhola Cool
- mood Great
- clothing Casual
21if Example 4
- weekend ( day Sunday ) ( day
Saturday ) - if ( weekend )
- bhola Cool
- mood Great
- clothing Casual
What is the data type of the variable weekend?
22We now know how to execute a statement or a block
of statements given that the condition is
trueWhat if we want to include an alternate
action as well, i.e. a statement or a block of
statements to be executed in case the condition
in not true
23if else Example 1
if ( GPA gt 1.0 ) bhola Pass else bhola
Fail
24if else Example 2
if ( GPA gt 1.0 ) bhola Pass
else bhola Fail
25if else Example 3
if ( GPA gt 1.0 ) bhola Pass mood
Great else bhola Fail
26if else Example 4
if ( GPA gt 1.0 ) bhola Pass mood
Great else bhola Fail mood
Terrible
27if else Example 5
if ( grade A ) points 4.0 if ( grade
B ) points 3.0 if ( grade C
) points 2.0 if ( grade D ) points
1.0 if ( grade F ) points 0.0
This piece of code is correct, but not very
efficient!
What can we do to improve it?
28Nested if else Structures
29if else Example 6
if ( grade A ) points 4.0 else if (
grade B ) points 3.0 else if (
grade C ) points 2.0 else if (
grade D ) points 1.0 else
points 0.0
30JavaScript Flow Control Structures
31switch Example 1
switch ( grade ) case A points 4.0
break case B points 3.0 break
case C points 2.0 break case
D points 1.0 break default
points 0.0
A colon following the case label is required
The expression enclosed in parentheses is
evaluated and matched with case labels
This is a case label
This break statement is the exit point
The default statement acts like the else
clause in the ifelse structure
32switch Example 2
switch ( inquiry ) 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! )
33if elseswitch
?
34ifelse --?-- switch
- If the action to be taken of the value of a
single variable (or a single expression), use
switch - When the action depends on the values of multiple
variables (or expressions), use the if...else
structure
35if else Example 7
if ( ( GPA gt 1.0 ) ( attendance gt 40 ) )
bhola Pass else if ( ( GPA gt 2.0 )
( attendance gt 36 ) ) bhola Probation
else bhola Fail
36Loops
37- Loop through a set of statements as long as a
condition is true
condition
True
statement block
False
38JavaScripts Looping Structures
39Decimal to Binary Conversion in JavaScript
The condition enclosed in parentheses
- x 75 // x is the decimal number
- y // y is the binary equivalent
- while ( x gt 0 )
- remainder x 2
- quotient Math.floor( x / 2 )
- y remainder y
- x quotient
-
- document.write( y y )
40while Example 2
- while ( tankIsFull false )
- tank tank bucket
-
- document.write ( Tank is full now )
41while Example 3
- x 1
- while ( x lt 6000 )
- document.write ( x )
- x x 1
42JavaScripts Looping Structures
43for Example 1
- x 1
- while ( x lt 6000 )
- document.write ( x )
- x x 1
Initial count
Condition
Operation
for ( x 1 x lt 6000 x x 1 )
document.write ( x )
44for Description (1)
- The for loop starts by initializing the counter
variable (which in this case is x) - The initial value in this case is 1, but can be
any other positive or negative number as well - Next the for loop checks the condition. If the
condition evaluates to a true value, the for
loop goes through the loop once
45for Description (2)
- After reaching the end of that iteration, the
for loop goes to the top once again, performs
the operation, checks the condition - If the condition evaluates to a false value,
the for loop finishes looping - Otherwise, the for loop goes through the loop
once again - Repeat from step 4
46for Example 2
- for ( x 99 x lt 6000 x x 1 )
- document.write ( x )
-
47for Example 3
- for ( x 6000 x gt 0 x x - 1 )
- document.write ( x )
-
How many iterations would this for loop run for?
6000?
48for Example 4
- for ( x 6000 x lt 0 x x - 1 )
- document.write ( x )
-
How many iterations would this for loop run for?
None?
49for while
?
50for --?-- while
- When the exact number of iterations is known, use
the for loop - When the number of iterations depend upon a
condition being met, use the while loop
51for loops become especially useful when used in
conjunction with arraysWell find out about
arrays next time, and well probe their
usefulness as part of for loop structures
52During Todays Lecture
- We discussed the concept of flow control using
the if and switch structures - And also the concept behind the while and for
looping structures - We also solved simple problems using flow control
and loop structures
53Next (the 9th) Web Dev LectureArrays
- We will find out why we need arrays
- We will become able to use arrays for solving
simple problems