Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 5: Control Structures II (Repetition)
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structures: continued.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Pertemuan 06 Repetition Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 9 - JavaScript: Control Statements II
Chapter 4 – C Program Control
Control Statements: Part 2
Chapter 4 - Program Control
Chapter 15 - JavaScript/JScript: Control Structures II
JavaScript: Control Statements I
JavaScript: Control Statements (II)
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 - Program Control
4 C Program Control.
Chapter 6 Control Statements: Part 2
Chapter 4 - Program Control
Presentation transcript:

Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5

Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : menghasilkan pemrograman web dengan menggunakan struktur seleksi dan pengulangan pada JavaScript (C3)

5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 The for Repetition Structure 5.4 Examples using the for Structure 5.5 The switch Multiple Selection Structure 5.6 The do/while Repetition Structure 5.7 The break and continue Statements 5.8 The Labeled break and continue statements 5.9 Logical Operators 5.10 Structured Programming Summary Outline Materi

5.1 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach to solve it When writing a script, important to –Understand types of building blocks and tools available –Employ proven program construction principles

5.2 Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: 1. Name of control variable (or loop counter) 2. Initial Value of control variable 3. Increment (or decrement) of control variable per loop 4. Condition that tests for final value of control variable Program readability: –Indent statements in body of each control structure –Blank line before and after each major control structure –Avoid more than three levels of nesting Sample Program

5.3 The for Repetition Structure for repetition structure: –Handles all details of counter-controlled repetition JavaScript statement: for ( var num = 1 ; i <= 7 ; i++ ) document.writeln( “ HTML Font size ” + num + “ ” ); continue..

5.3 The for Repetition Structure Equivalent Structures for structure: for ( initialization; loopContinuationTest ; increment ) statement; while structure: initialization; while ( loopContinuationTest ) { statement; increment; } continue..

5.3 The for Repetition Structure Flowchart: var num = 1 counter <= 7 document.writeln( “ HTML Font size ” + num + “ ” ); True False continue..

5.3 The for Repetition Structure Three expressions in for structure are optional –If loopContinuationTest omitted JavaScript assumes condition is true Leads to infinite loop –Can omit initialization expression if variable initialized elsewhere in program –Can omit increment statement if incrementation occurs inside structure If loop-continuation condition initially false, body of for structure not executed Delay loop –for structure empty except for semi-colon –Loop still runs specified number of times –Useful for slowing down programs, but more efficient techniques exist (Chapter 15) Sample Program

5.4 Examples using the for Structure Different methods for varying control variable in for structure Examples: –Control variable: 1 to 100, increments of 1 : for ( var i = 1; i <= 100; ++i ); –Control variable: 100 to 1, increments of –1 (decrements of 1 ): for ( var i = 100; i >= 1; --i ); –Control variable 7 to 77 :, steps of 7 : for ( var i = 7; i <= 77; i += 7 ); –Control variable over sequence of values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( var k = 99; k >= 0; k -= 11 ); continue.. Sample Program

5.4 Examples using the for Structure Math Object Math.pow( x, y ); –Calculates x raised to the y th power Math.round(); –Rounds the inputted value to the nearest integer –To output a number with to the second decimal place, use formula: Math.round( amount * 100 ) / 100 Example: Math.round( * 100 ) / 100 = 314/100 = 3.14 JavaScript represents all numbers as floating- point numbers –When floating-point numbers rounded, result may not be totally correct (especially when used in equations with other rounded values) Sample Program

5.5 The switch Multiple Selection Structure switch control structure –Contains multiple substructures –Actions executed depend on variable value –Works well classifying user inputs break statement –Skips to end of switch structure –Should be at the end of every case sub- structure –If left out, JavaScript will continue to test user input against cases continue..

5.5 The switch Multiple Selection Structure default case –Is executed if variable did not match any of the cases Good practices: –Test if user entered valid value –Indent all lines of structure continue..

5.5 The switch Multiple Selection Structure JavaScript statement: var choice; choice = window.prompt(); switch ( choice ) { case “a”: actions case “b”: actions case “z”: actions default: actions } continue..

5.5 The switch Multiple-Selection Structure Flowchart: case a action(s) case a case b case z true false case a action(s) break break action(s) Sample Program

5.6 The do/while Repetition Structure Similar to while control structure Difference –while : structure only executes if condition is initially true JavaScript statement: while ( condition ) { statement } –do/while : structure always executes at least once JavaScript statement: do { statement } while ( condition ); continue..

5.6 The do/while Repetition Structure Flowchart: true false condition action(s) Sample Program

5.7 The break and continue Statements Alter flow of control break; –Exits structure continue; –Skips remaining statements in structure; continues with next loop iteration When used properly –Performs faster than the corresponding structured techniques Sample Program1 Sample Program2

5.8 The Labeled break and continue statements break statement –Breaks out of immediately enclosing repetition control structure To break out of nested structures –Use labeled break statements –Begins with a label (identifier followed by colon) –Enclose structures to be broken out of within braces ({}) Called labeled compound statement –When executing break statement, follow format: break label; continue..

5.8 The Labeled break and continue statements Use of labeled continue statement –Follows same syntax and rules –After execution, continues with next iteration of enclosing labeled repetition structure Good practice to enter output statement to test if labeled statement executed properly Sample Program1 Sample Program2

5.9 Logical Operators Logical operators –Used to form more complex conditions by combining simple conditions Logical operators are –&& (logical AND) –|| (logical OR) –! (logical NOT or logical negation) continue..

5.9 Logical Operators && (logical AND) All statements connected by && operators in a condition must be true for condition to be true continue..

5.9 Logical Operators || (logical OR) Any statement connected by || operators in a condition must be true for condition to be true continue..

5.9 Logical Operators ! (logical NOT or logical negation) ! operator in front of a condition reverses the meaning of the condition. –A true value becomes false –A false value becomes true Sample Program

5.10 Structured Programming Summary Rules for Forming Structured Programs 1.Begin with the “simplest flowchart” 2.Any rectangle (action) can be replaced by two rectangles (actions) in sequence 3.Any rectangle (action) can be replaced by any control structure (sequence, if, if/else, switch, do/while or for ) 4.Rules 2 and 3 may be applied as often as you like and in any order continue..

5.10 Structured Programming Summary Repeatedly Applying Rule 2 to the Simplest Flowchart Rule 2 continue..

5.10 Structured Programming Summary Applying Rule 3 to the Simplest Flowchart Rule 3 continue..

5.10 Structured Programming Summary Structured approach: 7 single-entry/single- exit pieces –Selection control structures 1.if structure (single selection) 2.if/else structure (double selection) 3.switch structure (multiple selection) –Repetition control structures 4.while structure 5.do/while structure 6.for structure 7.for/in structure (Chap 12) continue..

5.10 Structured Programming Summary Any form of control in JavaScript can be expressed through –if structure (selection) –while structure (repetition) Control structures combined in two ways 1.Stacking 2.Nesting

End of Session 5