JavaScript: Control Statements.

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.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
© 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.
Fundamentals of C and C++ Programming Control Structures and Functions.
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.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and 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.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Chapter 7 JavaScript: Control Statements, Part 1
Branching statements.
Chapter 4 – C Program Control
The switch Statement, and Introduction to Looping
Control Statements: Part 1
JavaScript: Control Statements I
TMF1414 Introduction to Programming
CiS 260: App Dev I Chapter 4: Control Structures II.
Ch 7: JavaScript Control Statements I.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements I
JavaScript: Control Statements II
Chapter 4 Control Statements: Part I
MSIS 655 Advanced Business Applications Programming
Chapter 8 JavaScript: Control Statements, Part 2
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 4 - Program Control
3 Control Statements:.
Chapter 6 Control Statements: Part 2
Chapter 4: Control Structures I (Selection)
2.6 The if/else Selection Structure
JavaScript: Control Statements II
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
Chapter 8 JavaScript: Control Statements, Part 2
Control Statements:.
Presentation transcript:

JavaScript: Control Statements

Control Structures JavaScript provides three selection structures. The if statement either performs (selects) an action if a condition is true or skips the action if the condition is false. Called a single-selection structure because it selects or ignores a single action or group of actions. The if…else statement performs an action if a condition is true and performs a different action if the condition is false. Double-selection structure because it selects between two different actions or group of actions. The switch statement performs one of many different actions, depending on the value of an expression. Multiple-selection structure because it selects among many different actions or groups of actions.

Control Structures Cont: JavaScript provides four repetition statements, namely, while, do…while, for and for…in. Keywords cannot be used as identifiers (e.g., for variable names).

if...else Selection Statement Conditional operator (?:) Closely related to the if…else statement JavaScript’s only ternary operator—it takes three operands The operands together with the ?: operator form a conditional expression The first operand is a boolean expression The second is the value for the conditional expression if the boolean expression evaluates to true Third is the value for the conditional expression if the boolean expression evaluates to false

if...else Selection Statement (Cont.) Nested if…else statements Test for multiple cases by placing if…else statements inside other if…else structures The JavaScript interpreter always associates an else with the previous if, unless told to do otherwise by the placement of braces ({}) The if selection statement expects only one statement in its body To include several statements, enclose the statements in braces ({ and }) A set of statements contained within a pair of braces is called a block

if...else Selection Statement (Cont.) A logic error has its effect at execution time. A fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing, but the program produces incorrect results.

while while Repetition Statement Allows the programmer to specify that an action is to be repeated while some condition remains true The body of a loop may be a single statement or a block Eventually, the condition becomes false and repetition terminates

Formulating Algorithms: Counter-Controlled Repetition Often called definite repetition, because the number of repetitions is known before the loop begins executing A total is a variable in which a script accumulates the sum of a series of values Variables that store totals should normally be initialized to zero before they are used in a program A counter is a variable a script uses to count—typically in a repetition statement

Stores the sum of grades Sets total to 0 Sets gradeCounter to 1 in preparation for the loop Continues the cycle until gradeCounter is greater than 10

Increments gradeCounter by 1 after each iteration of the loop

Outer control structure Start nested control structure End nested control structure Increment for while loop

Additional control structure

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

Initializes counter Precedes the “ with a \ to create an escape sequence so that it can be used in the string Condition to be fulfilled with every iteration Incrementing statement

for Repetition Statement for statement Specifies each of the items needed for counter-controlled repetition with a control variable Can use a block to put multiple statements into the body If the loop’s condition uses a < or > instead of a <= or >=, or vice-versa, it can result in an off-by-one error for statement takes three expressions Initialization Condition Increment Expression The increment expression in the for statement acts like a stand-alone statement at the end of the body of the for statement Place only expressions involving the control variable in the initialization and increment sections of a for statement

Initial value of the control variable Condition to test whether looping should continue Increment to occur after each iteration of the loop Statement inside the for loop

Control variable year begins with a value of 1 Continue to execute the loop while year is less than or equal to 10 After each loop iteration, increase the value of year by 1

switch Multiple-Selection Statement switch statement Consists of a series of case labels and an optional default case When control reaches a switch statement The script evaluates the controlling expression in the parentheses Compares this value with the value in each of the case labels If the comparison evaluates to true, the statements after the case label are executed in order until a break statement is reached The break statement is used as the last statement in each case to exit the switch statement immediately The default case allows you to specify a set of statements to execute if no other case is satisfied Usually the last case in the switch statement

switch Multiple-Selection Statement (Cont.) Each case can have multiple actions (statements) Braces are not required around multiple actions in a case of a switch The break statement is not required for the last case because program control automatically continues with the next statement after the switch Having several case labels listed together (e.g., case 1: case 2: with no statements between the cases) executes the same set of actions for each case

Beginning of switch statement Beginning of statements to be executed if choice equals “1” Beginning of statements to be executed if choice equals “2” Statements Break out of switch statement

Beginning of statements to be executed if choice is anything other than “1”, “2” or “3” No break is necessary, since we’ve come to the end of the switch anyway

do…while Repetition Statement do…while statement tests the loop-continuation condition after the loop body executes The loop body always executes at least once

Perform the following actions… Then check to see if counter <= 6. If it is, iterate through the loop again.