JavaScript: Control Statements I Introduction JavaScript provides three

JavaScript: Control Statements I Introduction  JavaScript provides three

JavaScript: Control Statements I Introduction JavaScript provides three types of selection structures.

1. 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. 2. The ifelse (double-selection statement) selection statement performs an action if a condition is true and performs a different action if the condition is false.

3. The switch (multiple-selection statement) selection statement performs one of many different actions, depending on the value of an expression. JavaScript provides four repetition statementswhile, dowhile, for and forin.

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 ifelse Selection Statement The ifelse 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 IfElse statement may be written in JavaScript as Conditional Operator (?:) The operator ?: is JavaScripts only ternary operatorit 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 ifelse statements test for multiple cases by placing ifelse statements inside ifelse 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: 1. The name of a control variable (or loop counter). 2. The initial value of the control variable.

3. The increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop). 4. 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 (numbersthe default), lower-roman (lowercase Roman numerals), upper-roman (uppercase Roman numerals), lower-alpha (lowercase letters), upper-alpha (uppercase letters), and more. dowhile 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 dowhile statement tests the loop-continuation condition after the loop body executestherefore, the loop body always executes at least once.

break and continue Statements break Statement The break statement, when executed in a while, for, dowhile 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 dowhile statement, skips the

remaining statements in the body of the statement and proceeds with the next iteration of the loop. In while and dowhile statements, the loopcontinuation 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

Recently Viewed Presentations

  • The Milky Way

    The Milky Way

    Superclusters form filaments and walls around voids. scientific notation solar system planet star astronomical unit (AU) light-year (ly) galaxy Milky Way Milky Way Galaxy spiral arm * * Title Author
  • Triangles - Ms.Perez's Site

    Triangles - Ms.Perez's Site

    Acute Triangle. Each angle is acute. All angles less than 90° Obtuse Triangle. One angle is obtuse. It has an angle greater than 90° Right Triangle. One angle is a right angle. One angle is exactly 90°
  • Social Media

    Social Media

    From the Founder of General Systems Theory. Ludwig von Bertalanffy. A system may be defined as a set of elements standing in interrelation among themselves and with environment. We term a system 'closed' if no material enters or leave it;...
  • Kuali Student at UBC

    Kuali Student at UBC

    SIS doesn't support non-credit courses / experiential learning. Forced to use workarounds (special topics, directed studies) Students and faculty frustrated by courses listed but not offered. Organizations, People. ... Kuali Student at UBC
  • Diapositive 1 - WIU

    Diapositive 1 - WIU

    Point-to-point tunneling protocol (PPTP) Protocol of PPP encapsulation over IP designed by Microsoft. Allow to set the virtual private networks (VPNs) over a public network. The protocol opens two channels of communication between the client and server: a channel control...
  • Claiborne County Schools Annual Harassment Training REVIEWED JULY

    Claiborne County Schools Annual Harassment Training REVIEWED JULY

    Boys tend to bully with direct bullying or physical or verbal aggression. Girls tend to bully with indirect means such as social aggression. Boys who bully tend to be 1 to 2 years older than their victims. Their victims can...
  • Title… - United Nations Statistics Division

    Title… - United Nations Statistics Division

    The Statistical Database covers the region and contains over 600 data series on a wide range of topics on population, education, health, poverty and inequalities, gender, economy, environment and connectivity
  • Introduction to Computing CS A109 Course Objectives  Goal

    Introduction to Computing CS A109 Course Objectives Goal

    Introduction to Computing CS A109 Course Objectives Goal is to teach computation in terms relevant to non-CS majors Students will be able to read, understand, modify, and assemble from pieces programs that achieve useful communication tasks: Image manipulation, sound synthesis...