JavaScript: Control Statements, Part 2JavaScript: جمل التحكم، الجزء الثاني
مقدمة الفصل
ماذا ستتعلم في هذا الفصل
- Learn the essentials of counter-controlled repetition.
- Use the for and do…while repetition statements to execute statements in a program repeatedly.
- Perform multiple selection using the switch selection statement.
- Use the break and continue program-control statements.
- Use the logical operators to make decisions.
أقسام الفصل
- 1Essentials of counter-controlled repetitionأساسيات التكرار المتحكم بعداد
- 2The for repetition statementجملة التكرار for
- 3Examples using the for statementأمثلة على for
- 4switch multiple-selection statementجملة الاختيار المتعدد switch
- 5do…while repetition statementجملة التكرار do…while
- 6break and continue statementsجملتا break و continue
- 7Logical operatorsالعوامل المنطقية
تلميح: استخدم أسهم لوحة المفاتيح للتنقل بين الشرائح.
Essentials of counter-controlled repetitionأساسيات التكرار بعداد
- 1Name of a control variable
- 2Initial value of the control variable
- 3Increment (or decrement)by which the control variable is modified each time through the loop
- 4Condition that tests for the final valueto determine whether looping should continue
- The double-quote character delimits a string literal; inside a string, write it as the escape sequence \".
- HTML5 allows single (') or double (") quotes around an attribute value; JavaScript allows single quotes inside a string literal.
The for repetition statementجملة for
Specifies each of the items needed for counter-controlled repetition with a control variable. Its header contains three expressions: initialization; condition; increment expression.
- The increment expression acts like a stand-alone statement at the end of the body.
- Place only expressions involving the control variable in the initialization and increment sections.
- The three expressions are optional; the two semicolons are required; the portions can contain arithmetic expressions.
for: pitfalls and rulesقواعد for وأخطاؤها
The for repetition statement
Can result if the loop's condition uses a < or > instead of a <= or >=, or vice-versa.
The part of a script in which a variable name can be used.
A negative "increment": the loop actually counts downward.
- If the loop-continuation condition initially is false, the body is not performed; execution proceeds with the statement following the for.
Examples using the for statementأمثلة for
المسألة
Fig. 8.5: sum the even integers from 2 to 100.
خطوات الحل
- var sum = 0;
- for ( var number = 2; number <= 100; number += 2 ) sum += number;
- document.writeln( "The sum of the even integers from 2 to 100 is " + sum );
النتيجة
The sum of the even integers from 2 to 100 is 2550 (output on slide 13).
- JavaScript does not include an exponentiation operator: Math.pow(x, y) calculates x raised to the y th power (slide 17).
- Fig. 8.6 (compound interest, slides 14–16): principal = 1000.00, rate = 0.05; for ( var year = 1; year <= 10; ++year ) amount = principal * Math.pow( 1.0 + rate, year ); each amount is written with amount.toFixed(2), one table row per year.
switch multiple-selection statementجملة switch
Tests a variable or expression separately for each of the values it may assume; different actions are taken for each value. Consists of a series of case labels and an optional default case.
When control reaches a switch (slide 19)
- 1
The script evaluates the controlling expression in the parentheses.
- 2
Compares this value with the value in each of the case labels.
- 3
If the comparison evaluates to true, the statements after the case label are executed in order until a break statement is reached.
- 4
If no other case is satisfied, the default case runs (usually the last case).
switch rules and the list exampleقواعد switch ومثال القوائم
switch multiple-selection statement
- Each case can have multiple actions; braces are not required around them.
- break is not required for the last case: control automatically continues with the next statement after the switch.
- Several case labels listed together (e.g., case 1: case 2:) execute the same set of actions for each case.
CSS list-style-type values (slide 18)
- decimal (default)
- lower-roman
- upper-roman
- lower-alpha
- upper-alpha
do…while repetition statementجملة do…while
Tests the loop-continuation condition after the loop body executes — the loop body always executes at least once.
break and continuebreak و continue
break
- In while, for, do…while or switch
- Immediate exit from the statement
- Execution continues with the next statement in sequence
- Uses: escape early from a loop; skip the remainder of a switch
continue
- In while, for or do…while
- Skips the remaining statements in the body
- Proceeds with the next iteration
- while / do…while: the test evaluates immediately; for: the increment expression executes, then the test
سؤال سريع
break and continue statements
Logical operatorsالعوامل المنطقية
Ensures that two conditions are both true before choosing a certain path of execution.
Ensures that either or both of two conditions are true before choosing a certain path of execution.
Reverses the meaning of a condition; has only a single condition as an operand (a unary operator).
| expression1 | expression2 | expression1 && expression2 | expression1 || expression2 |
|---|---|---|---|
| false | false | false | false |
| false | true | false | true |
| true | false | false | true |
| true | true | true | true |
- Fig. 8.15: !false is true; !true is false.
- JavaScript evaluates to false or true all expressions that include relational, equality and/or logical operators.
Precedence and short-circuit evaluationالأسبقية والتقييم المختصر
Logical operators
- && has a higher precedence than ||. Both associate from left to right.
- An expression containing && or || is evaluated only until truth or falsity is known: short-circuit evaluation.
| Operator | Associativity | Type |
|---|---|---|
| ++ -- ! | right to left | unary |
| * / % | left to right | multiplicative |
| + - | left to right | additive |
| < <= > >= | left to right | relational |
| == != === !== | left to right | equality |
| && | left to right | logical AND |
| || | left to right | logical OR |
| ?: | right to left | conditional |
| = += -= *= /= %= | right to left | assignment |
Nonboolean values as true or falseالقيم كصحيح أو خطأ
Logical operators
Considered true
- Nonzero numeric values
- Any string that contains characters
- All objects
Considered false
- The numeric value zero
- The empty string
- The value null
- Variables that have been declared but not initialized
الخلاصة
أنهيت الدرس
الخطوة التالية: ثبّت المعلومات بالبطاقات، ثم اختبر نفسك بالأسئلة.
ما راجعته في هذا الدرس
- Essentials of counter-controlled repetitionأساسيات التكرار المتحكم بعداد
- The for repetition statementجملة التكرار for
- Examples using the for statementأمثلة على for
- switch multiple-selection statementجملة الاختيار المتعدد switch
- do…while repetition statementجملة التكرار do…while
- break and continue statementsجملتا break و continue
- Logical operatorsالعوامل المنطقية