Transcript

Kelompok 8 Java Script : Control Statement II

Kelompok 8 Java Script : Control Statement II

Oleh:Kukuh I. 5108100122Brian M. 5108100156Yodiar Hellian 5108100165

Chapter 8 - JavaScript: Control Statements IIChapter 8 - JavaScript: Control Statements II

1. Introduction2. Essentials of Counter-Controlled Repetition3. For Repetition Statement4. Examples Using the for Statement5. Switch Multiple-Selection Statement6. Do…while Repetition Statement7. Break and continue Statements8. Labeled break and continue Statements9. Logical Operators

1. Introduction

• Continuation of Chapter 8– Theory and principles of structured programming

2. Essentials of Counter-Controlled Repetition

• Counter-controlled repetition– Name of a control– Initial value– Increment or decrement– Final value

While Counter.html

3. for Repetition Statement

• for repetition statement– Handles all the details of counter-controlled

repetition– for structure header

• The first line

3. for Repetition Statement

for ( var counter=1 ; counter<=7 ; ++counter )

Initial value of control variable Increment control variable

Control variable name Final value of control variable for which the condition is true

for keyword

Loop-continuation condition

3. for Repetition Statement

counter <= 7

document.writeln( "<p style=\"font-size: " + counter + "ex\">XHTML font size " + counter + "ex</p>" );

true

false

var counter = 1

++counter

Establish initial valueof control variable.

Determine if final value of control variable has been reached.

Body of loop (this may be many statements)

Increment the control variable.

Fig. 9.4 for repetition structure flowchart.

For Counter.html

4. Examples Using the for Statement

• Summation with for• Compound interest calculation with for loop

– Math object• Method pow• Method round

Sum.html

Interest.html

5. switch Multiple-Selection Statement

• Controlling expression• Case labels• Default case

SwitchTest.html

5. switch Multiple-Selection Statement

case a case a action(s)true

false

.

.

.

break

case b action(s) break

false

false

case z case z action(s) break

default action(s)

true

true

case b

6. do…while Repetition Statement

• Similar to the while statement• Tests the loop continuation condition after the

loop body executes• Loop body always executes at least once

6. do…while Repetition Structure

conditiontrue

action(s)

false

Fig. 9.10 do…while repetition statement flowchart.

DoWhile.html

7. break and continue Statements

• break

– Immediate exit from the structure– Used to escape early from a loop– Skip the remainder of a switch statement

• continue

– Skips the remaining statements in the body of the structure

– Proceeds with the next iteration of the loop

BreakTest.html

ContinueTest.html

8. Labeled break and continue Statements

• Labeled break statement– Break out of a nested set of structures– Immediate exit from that structure and enclosing

repetition structures– Execution resumes with first statement after enclosing

labeled statement• Labeled continue statement

– Skips the remaining statements in structure’s body and enclosing repetition structures

– Proceeds with next iteration of enclosing labeled repetition structure

– Loop-continuation test evaluates immediately after the continue statement executes

BreakLabelTest.html

ContinueLabelTest.html

9. Logical Operators

• More logical operators– Logical AND ( && )– Logical OR ( || )– Logical NOT ( ! )

9 Logical Operators

expression1 expression2 expression1 && expression2

false false false false true false true false false true true true

9. Logical Operatorsexpression1 expression2 expression1 ||

expression2 false false false false true true true false true true true true

expression !expression false true true false

LogicalOperators.html

top related