JavaScript: Control Statements I

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © 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.
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
 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.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
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 (for) Outline 4.1Introduction 4.2The.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 7 JavaScript: Control Statements, Part 1
Branching statements.
Introduction to Computer Programming
Java Fundamentals 4.
Chapter 4 – C Program Control
Control Statements: Part 1
Control Statements: Part 2
JavaScript: Control Statements I
Chapter 4 - Program Control
TMF1414 Introduction to Programming
Chapter 8 - JavaScript: Control Statements I
Ch 7: JavaScript Control Statements I.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
JavaScript: Control Statements II
Chapter 4 Control Statements: Part I
Chapter 5- part 2 Control Statements: Loops 2
JavaScript: Control Statements (II)
MSIS 655 Advanced Business Applications Programming
Chapter 8 JavaScript: Control Statements, Part 2
Structured Program Development in C
Chapter 4 - Program Control
3 Control Statements:.
Chapter 6 Control Statements: Part 2
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
Chapter 8 JavaScript: Control Statements, Part 2
Control Statements:.
Presentation transcript:

JavaScript: Control Statements I

Introduction JavaScript provides three types of selection structures. The if selection (single-selection statement) statement performs (selects) an action if a condition is true or skips the action if the condition is false. The if…else (double-selection statement) selection statement performs an action if a condition is true and performs a different action if the condition is false. The switch (multiple-selection statement) selection statement performs one of many different actions, depending on the value of an expression. JavaScript provides four repetition statements—while, do…while, for and for…in.

if Selection Statement A selection statement is used to choose among alternative courses of action in a script. For example: The preceding pseudocode If statement can be written in JavaScript as

if…else Selection Statement The if…else selection statement allows you to specify that a different action is to be performed when the condition is true than when the condition is false. For example: The preceding pseudocode If…Else statement may be written in JavaScript as

Conditional Operator (?:) The operator ?: is JavaScript’s only ternary operator—it takes three operands. The operands together with the ?: form a conditional expression. The first operand is a boolean expression, the second is the value for the conditional expression if the expression evaluates to true and the third is the value for the conditional expression if the expression evaluates to false.

Nested if...else Statements Nested if…else statements test for multiple cases by placing if…else statements inside if…else statements.

Dangling-else Problem The following code illustrates the dangling-else problem. For example, The interpreter interprets the preceding statement as To force the first nested if statement to execute as it was intended originally, we must write it as follows: The braces ({}) indicate to the JavaScript interpreter that the second if statement is in the body of the first if statement and that the else is matched with the first if statement.

while Repetition Statement A repetition structure (also known as a loop) allows you to specify that a script is to repeat an action while some condition remains true. The statement is as follows:

Assignment Operators JavaScript provides several additional assignment operators (called compound assignment operators) for abbreviating assignment expressions. Examples Any statement of the form where operator is one of the binary operators +, -, *, / or %

Increment and Decrement Operators

Formulating Algorithms: Counter-Controlled Repetition Consider the following problem statement:

Formulating Algorithms: Sentinel-Controlled Repetition Consider the following problem: Develop a class-averaging script that will process an arbitrary number of grades each time the script is run.

Nested Control Statements

Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: The name of a control variable (or loop counter). The initial value of the control variable. The increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop). The condition that tests for the final value of the control variable to determine looping should continue.

for Repetition Statement

General Format of a for Statement

Summing Integers with a for Statement

Calculating Compound Interest with the for Statement Consider the following problem statement:

switch Multiple-Selection Statement

We create these different lists using the CSS property list-style-type, which allows us to set the numbering system for the list. Possible values include decimal (numbers—the default), lower-roman (lowercase Roman numerals), upper-roman (uppercase Roman numerals), lower-alpha (lowercase letters), upper-alpha (uppercase letters), and more.

do…while Repetition Statement In the while statement, the loop-continuation test occurs at the beginning of the loop, before the body of the loop executes. The do…while statement tests the loop-continuation condition after the loop body executes—therefore, the loop body always executes at least once.

break and continue Statements break Statement The break statement, when executed in a while, for, do…while or switch statement, causes immediate exit from the statement. Execution continues with the first statement after the structure.

continue Statement The continue statement, when executed in a while, for or do…while statement, skips the remaining statements in the body of the statement and proceeds with the next iteration of the loop. In while and do…while statements, the loop-continuation test evaluates immediately after the continue statement executes. In for statements, the increment expression executes, then the loop- continuation test evaluates. Improper placement of continue before the increment in a while may result in an infinite loop.

Logical Operators JavaScript provides logical operators that can be used to form more complex conditions by combining simple conditions. The logical operators are && (logical AND), || (logical OR) and ! (logical NOT, also called logical negation). && (Logical AND) Operator

|| (Logical OR) Operator ! (Logical Negation) Operator