JavaScript: Control Statements, Part 1JavaScript: جمل التحكم، الجزء الأول

الشريحة 1 من 17

مقدمة الفصل

ماذا ستتعلم في هذا الفصل

  1. Learn basic problem-solving techniques.
  2. Develop algorithms through the process of top-down, stepwise refinement.
  3. Use the if and if…else selection statements to choose among alternative actions.
  4. Use the while repetition statement to execute statements in a script repeatedly.
  5. Implement counter-controlled repetition and sentinel-controlled repetition.
  6. Use the increment, decrement and assignment operators.
تفاصيل المصدرالفصل 8 · الشريحة ⁦3⁩
المصدر
الفصل 8 · الشريحة ⁦3⁩

Algorithms and pseudocodeالخوارزميات والكود الوصفي

Algorithm
A procedure for solving a problem in terms of the actions to be executed and the order in which the actions are to be executed.
Pseudocode
An informal language that helps you develop algorithms — similar to everyday English, convenient and user friendly, but not an actual computer programming language.
تفاصيل المصدرالفصل 8 · الشرائح ⁦5–6⁩
المصدر
الفصل 8 · الشرائح ⁦5–6⁩

سؤال سريع

Algorithms and pseudocode

Which statement about algorithms and pseudocode is correct?

تفاصيل المصدرالفصل 8 · الشرائح ⁦5–6⁩
المصدر
الفصل 8 · الشرائح ⁦5–6⁩

Control statementsجمل التحكم

  • Sequential execution: statements run in the order they appear. Transfer of control: changing that order.
  • All scripts can be written with only three control statements: sequence, selection, repetition.
StatementالجملةKind (slides 8–9)النوع
ifSingle-selection: performs an action if a condition is true, or skips it
if…elseDouble-selection: one action if true, a different action if false
switchMultiple-selection: one of many actions depending on the value of an expression
while, do…while, for, for…inThe four repetition statements
  • Reserved words also include the values null, true and false.
تفاصيل المصدرالفصل 8 · الشرائح ⁦7–9⁩
المصدر
الفصل 8 · الشرائح ⁦7–9⁩

سؤال سريع

Control statements

Why is switch called a multiple-selection statement?

تفاصيل المصدرالفصل 8 · الشريحة ⁦8⁩
المصدر
الفصل 8 · الشريحة ⁦8⁩

if, if…else and ?:if و if…else و ?:

  • The interpreter ignores white space; use a uniform indentation convention.
  • A decision can be made on any expression that evaluates to true or false.
  • if…else specifies different actions when the condition is true and when it is false.
Conditional operator ?:
JavaScript's only ternary operator (three operands): a boolean expression, the value if it is true, and the value if it is false.
تفاصيل المصدرالفصل 8 · الشرائح ⁦11–13⁩
المصدر
الفصل 8 · الشرائح ⁦11–13⁩

سؤال سريع

if, if…else and the conditional operator

var result = ( mark >= 60 ? "Pass" : "Fail" ); If mark is 45, what is result?

تفاصيل المصدرالفصل 8 · الشريحة ⁦13⁩
المصدر
الفصل 8 · الشريحة ⁦13⁩

Nested if…else, blocks and logic errorsالتداخل والكتل والأخطاء

if, if…else and the conditional operator

  • Nested if…else statements test multiple cases.
  • The interpreter always associates an else with the previous if, unless braces ({}) say otherwise.
  • An if body holds one statement; several statements go in braces — a block.

Fatal logic errorخطأ قاتل

  • Script fails and terminates prematurely

Nonfatal logic errorخطأ غير قاتل

  • Script continues but produces incorrect results
تفاصيل المصدرالفصل 8 · الشرائح ⁦14–15⁩
المصدر
الفصل 8 · الشرائح ⁦14–15⁩

سؤال سريع

if, if…else and the conditional operator

if ( x > 5 )if ( y > 5 )document.writeln( "A" );elsedocument.writeln( "B" );With x = 3 and y = 9 and no braces, what is written?

تفاصيل المصدرالفصل 8 · الشريحة ⁦14⁩
المصدر
الفصل 8 · الشريحة ⁦14⁩

while and counter-controlled repetitionwhile والتكرار بعداد

while
Repeats an action while some condition remains true. The body may be a single statement or a block; eventually the condition becomes false and repetition terminates.
Counter-controlled repetition
Often called definite repetition, because the number of repetitions is known before the loop begins.
Total
Accumulates the sum of a series of values; normally initialized to zero.
Counter
A variable used to count, typically in a repetition statement.
تفاصيل المصدرالفصل 8 · الشرائح ⁦16–17⁩
المصدر
الفصل 8 · الشرائح ⁦16–17⁩

سؤال سريع

while and counter-controlled repetition

A teacher's script reads exactly 25 grades for a class of 25 students. Which kind of repetition is this?

تفاصيل المصدرالفصل 8 · الشريحة ⁦17⁩
المصدر
الفصل 8 · الشريحة ⁦17⁩

Sentinel-controlled repetitionالتكرار بقيمة حارسة

Sentinel value
A special value (also a signal, dummy or flag value) that indicates the end of data entry.

Counter-controlledبعداد

  • Definite repetition
  • Number of repetitions known in advance

Sentinel-controlledبقيمة حارسة

  • Indefinite repetition
  • Number of repetitions not known in advance
  • Choose a sentinel that cannot be confused with an acceptable input value.
  • Control statements may be stacked in sequence or nested inside one another.
تفاصيل المصدرالفصل 8 · الشرائح ⁦22, 23, 27⁩
المصدر
الفصل 8 · الشرائح ⁦22, 23, 27⁩

سؤال سريع

Sentinel-controlled repetition and nesting

A script reads exam scores (0–100) until the user types a value meaning "no more scores". Which value is the best sentinel?

تفاصيل المصدرالفصل 8 · الشريحة ⁦22⁩
المصدر
الفصل 8 · الشريحة ⁦22⁩

Assignment, increment and decrementالإسناد والزيادة والنقصان

  • Arithmetic assignment operators +=, -=, *=, /=, %= abbreviate common expressions.
Fig. 7.13 — Increment and decrement operators
OperatorالعاملExampleمثالCalledالاسمExplanation (Fig. 7.13)الشرح
++++apreincrementIncrement a by 1, then use the new value of a in the expression
++a++postincrementUse the current value of a in the expression, then increment a by 1
----bpredecrementDecrement b by 1, then use the new value of b in the expression
--b--postdecrementUse the current value of b in the expression, then decrement b by 1
تفاصيل المصدرالفصل 8 · الشرائح ⁦32, 33, 34, 37⁩
المصدر
الفصل 8 · الشرائح ⁦32, 33, 34, 37⁩

سؤال سريع

Assignment, increment and decrement operators

var c = 7; document.writeln( c++ ); document.writeln( ++c ); What two values are written?

تفاصيل المصدرالفصل 8 · الشريحة ⁦34⁩
المصدر
الفصل 8 · الشريحة ⁦34⁩

Precedence and associativity so farالأسبقية والتجميع

Assignment, increment and decrement operators

Fig. 7.15 — Precedence and associativity of the operators discussed so far
OperatorالعاملAssociativityاتجاه التجميعTypeالنوع
++ --right to leftunary
* / %left to rightmultiplicative
+ -left to rightadditive
< <= > >=left to rightrelational
== != === !==left to rightequality
?:right to leftconditional
= += -= *= /= %=right to leftassignment
تفاصيل المصدرالفصل 8 · الشريحة ⁦38⁩
المصدر
الفصل 8 · الشريحة ⁦38⁩

الخلاصة

تفاصيل المصدر
المصدر
الفصل 8

خريطة الفصل