• No results found

Lec 13 JavaScript - III.pptx

N/A
N/A
Protected

Academic year: 2020

Share "Lec 13 JavaScript - III.pptx"

Copied!
44
0
0

Loading.... (view fulltext now)

Full text

(1)

CS1113 Web Programming

Lecture 13

Flow Control & Loops

(2)

2

Today’s Lecture:

Flow Control & Loops

We’ll 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

(3)

Flow

(4)

4

Select between alternate courses of action

depending upon the evaluation of a condition

statement block 1

condition

True False

(5)

JavaScript Flow Control Structures

if … else

(6)

6

if: Example 1

if ( day == “Sunday” ) mood = “Cool” ;

Set the value of the variable ‘mood to ‘Cool’ if the ‘day’ is equal to ‘Sunday’

The condition enclosed in

(7)

This was the case if we want to

execute a

single statement

given that

the condition is true

(8)

8

if: Example 2

if ( day == “Sunday” ) { mood = “Cool” ;

emotion = “Great” ; clothing = “Casual” ; }

Set the value of the variable ‘mood to ‘Cool’, ‘emotion’ to ‘Great’, and ‘clothing’ to ‘casual’ if the ‘day’ is equal to ‘Sunday’

(9)

if: Example 2

if ( day == “Sunday” ) { mood = “Cool” ;

emotion = “Great” ; clothing = “Casual” ;

} Note: No semicolon

(10)

10

Compound Statements

1. At times, we need to put multiple statements at places where JavaScript expects only one

2. For those situations, JavaScript provides a

(11)

Compound Statements

3. This is done simply by enclosing any number of statements within curly braces, { }

4. NOTE: Although the statements within the block end in semicolons, the block itself

(12)

12

if: Example 3

if ( (day == “Sunday”) || (day == “Saturday”) ) { mood = “Cool” ;

(13)

if: Example 4

weekend = ( day == “Sunday” ) || ( day == “Saturday” ) ;

if ( weekend ) { mood = “Cool” ;

emotion = “Great” ; clothing = “Casual” ;

(14)

14

We now know how to execute a

statement or a

block of statements

given that the condition is true

What if we want to include an

alternate

action as well,

i.e. a statement or a

(15)

if … else: Example 1

if ( GPA >= 2.5 ) akash = “Pass” ; else

(16)

16

if … else: Example 2

if ( GPA >= 2.5 ) { akash = “Pass” ; }

else

(17)

if … else: Example 3

if ( GPA >= 2.5 ) { akash = “Pass” ; mood = “Great” ; }

else

(18)

18

if … else: Example 4

if ( GPA >= 2.5 ) { akash = “Pass” ; mood = “Great” ; }

else {

akash = “Fail” ;

(19)

if … else: Example 5

if ( grade == “A” ) points = 4.0 ; if ( grade == “B” )

points = 3.0 ; if ( grade == “C” )

points = 2.0 ;

if ( grade == “D” ) What can we do to

This piece of code is

correct, but not very

(20)

20

Nested

if … else

(21)

if … 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” )

(22)

22

JavaScript Flow Control Structures

if … else

(23)

switch:

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 ;

The expression enclosed in parentheses is evaluated and matched with case labels This is a

(24)

24

switch: 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 :

(25)

?

if … else

(26)

26

if…else --?-- 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

(27)

if … else: Example 7

if ( ( GPA >= 2.5 ) && ( attendance >= 75 ) ) akash = “Pass” ;

else {

if ( ( GPA >= 2.0 ) && ( attendance >= 36 ) ) akash = “Probation” ;

(28)

28

(29)

Loop through a set of statements

as long as a condition is true

condition True

(30)

30

JavaScript’s Looping Structures

while

for

(31)

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

;

Decimal to Binary Conversion in JavaScript

(32)

32

while: Example 2

while (

tankIsFull

==

false

) {

tank

=

tank

+

bucket

;

}

(33)

while: Example 3

x

=

1

;

while (

x

<

6000

) {

document.write (

x

) ;

(34)

34

JavaScript’s Looping Structures

while

for

(35)

for: Example 1

x

=

1

;

while (

x

<

6000

)

{

document.write (

x

) ;

x

=

x

+

1

;

}

for (

x

=

1

;

x

<

6000

;

x

=

x

+

1

) {

document.write (

x

) ;

(36)

36

for: Description (1)

1. The ‘for’ loop starts by initializing the counter variable (which in this case is x)

2. The initial value in this case is ‘1’, but can be any other positive or negative number as well

(37)

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

5. If the condition evaluates to a ‘false’ value, the ‘for’ loop finishes looping

(38)

38

for: Example 2

for (

x

=

99

;

x

<

6000

;

x

=

x

+

1

) {

document.write (

x

) ;

(39)

for: Example 3

for (

x

=

6000

;

x

>

0

;

x

=

x

-

1

) {

document.write (

x

) ;

}

How many

(40)

40

for: Example 4

for (

x

=

6000

;

x

<

0

;

x

=

x

-

1

) {

document.write (

x

) ;

}

How many

iterations would this ‘for’ loop

(41)
(42)

42

for --?-- while

When the exact number of iterations is

known, use the ‘for’ loop

When the number of iterations depend

(43)

‘for’ loops become

especially useful

when used

in conjunction with arrays

(44)

44

During Today’s 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

References

Related documents

The analysis’ results showed that the prognostic value of exon 2/3 K-Ras mutations is retained after correction for other well-known prognostic factors (age at diagnosis, tumor

However, there will be times when you need to interact more with the data. You might need to filter based 

[r]

We implemented this using a Symmetrix VMAX 10K storage array together with an existing VNX5300 storage array to form a bi-directional disaster recovery solution.. We achieved this

Pulse Rate Measurement During Sleep Using Wearable Sensors, and its Correlation with the Menstrual Cycle Phases, A Prospective Observational Study... Pulse Rate Measurement During

Within the 180 days before the filing of my bankruptcy case, I received a briefing from a credit counseling agency approved by the United States trustee or bankruptcy administrator

4.3 – Hydrostatic Conditions – Slab to Wall Transition To begin installation up the wall in a hydrostatic shotcrete application, take a transition sheet of Aussie Clay 590-PL

b for the latter an asymmetric adjustment to the long run equilibrium relation should be accounted for since the estimate of the Wald test statistics under the null of