JavaScript: Control Statements, Part 1JavaScript: جمل التحكم، الجزء الأول
مقدمة الفصل
ماذا ستتعلم في هذا الفصل
- Learn basic problem-solving techniques.
- Develop algorithms through the process of top-down, stepwise refinement.
- Use the if and if…else selection statements to choose among alternative actions.
- Use the while repetition statement to execute statements in a script repeatedly.
- Implement counter-controlled repetition and sentinel-controlled repetition.
- Use the increment, decrement and assignment operators.
أقسام الفصل
- 1Algorithms and pseudocodeالخوارزميات والكود الوصفي
- 2Control statementsجمل التحكم
- 3if, if…else and the conditional operatorif و if…else والعامل الشرطي
- 4while and counter-controlled repetitionwhile والتكرار بعداد
- 5Sentinel-controlled repetition and nestingالتكرار بقيمة حارسة والتداخل
- 6Assignment, increment and decrement operatorsعمليات الإسناد والزيادة والنقصان
تلميح: استخدم أسهم لوحة المفاتيح للتنقل بين الشرائح.
Algorithms and pseudocodeالخوارزميات والكود الوصفي
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.
An informal language that helps you develop algorithms — similar to everyday English, convenient and user friendly, but not an actual computer programming language.
سؤال سريع
Algorithms and pseudocode
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)النوع |
|---|---|
| if | Single-selection: performs an action if a condition is true, or skips it |
| if…else | Double-selection: one action if true, a different action if false |
| switch | Multiple-selection: one of many actions depending on the value of an expression |
| while, do…while, for, for…in | The four repetition statements |
- Reserved words also include the values null, true and false.
سؤال سريع
Control statements
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.
JavaScript's only ternary operator (three operands): a boolean expression, the value if it is true, and the value if it is false.
سؤال سريع
if, if…else and the conditional operator
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
سؤال سريع
if, if…else and the conditional operator
while and counter-controlled repetitionwhile والتكرار بعداد
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.
Often called definite repetition, because the number of repetitions is known before the loop begins.
Accumulates the sum of a series of values; normally initialized to zero.
A variable used to count, typically in a repetition statement.
سؤال سريع
while and counter-controlled repetition
Sentinel-controlled repetitionالتكرار بقيمة حارسة
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.
سؤال سريع
Sentinel-controlled repetition and nesting
Assignment, increment and decrementالإسناد والزيادة والنقصان
- Arithmetic assignment operators +=, -=, *=, /=, %= abbreviate common expressions.
| Operatorالعامل | Exampleمثال | Calledالاسم | Explanation (Fig. 7.13)الشرح |
|---|---|---|---|
| ++ | ++a | preincrement | Increment a by 1, then use the new value of a in the expression |
| ++ | a++ | postincrement | Use the current value of a in the expression, then increment a by 1 |
| -- | --b | predecrement | Decrement b by 1, then use the new value of b in the expression |
| -- | b-- | postdecrement | Use the current value of b in the expression, then decrement b by 1 |
سؤال سريع
Assignment, increment and decrement operators
Precedence and associativity so farالأسبقية والتجميع
Assignment, increment and decrement operators
| Operatorالعامل | Associativityاتجاه التجميع | Typeالنوع |
|---|---|---|
| ++ -- | right to left | unary |
| * / % | left to right | multiplicative |
| + - | left to right | additive |
| < <= > >= | left to right | relational |
| == != === !== | left to right | equality |
| ?: | right to left | conditional |
| = += -= *= /= %= | right to left | assignment |
الخلاصة
أنهيت الدرس
الخطوة التالية: ثبّت المعلومات بالبطاقات، ثم اختبر نفسك بالأسئلة.
ما راجعته في هذا الدرس
- Algorithms and pseudocodeالخوارزميات والكود الوصفي
- Control statementsجمل التحكم
- if, if…else and the conditional operatorif و if…else والعامل الشرطي
- while and counter-controlled repetitionwhile والتكرار بعداد
- Sentinel-controlled repetition and nestingالتكرار بقيمة حارسة والتداخل
- Assignment, increment and decrement operatorsعمليات الإسناد والزيادة والنقصان