Trình bày về chủ đề: "JavaScript: Câu lệnh điều khiển I" - Bảng điểm trình bày:
1 JavaScript: Câu lệnh kiểm soát I JavaScript: Control Statements I
2 Giới thiệu JavaScript cung cấp ba loại cấu trúc lựa chọn. Câu lệnh IF SELLE [Tuyên bố lựa chọn đơn] Thực hiện [chọn] một hành động nếu một điều kiện là đúng hoặc bỏ qua hành động nếu điều kiện là sai. ] Câu lệnh Lựa chọn thực hiện một hành động nếu một điều kiện là đúng và thực hiện một hành động khác nếu điều kiện là sai. Công tắc [câu lệnh đa hướng] Câu lệnh thực hiện một trong nhiều hành động khác nhau, tùy thuộc vào giá trị của biểu thức.JavaScript cung cấp bốn Các tuyên bố lặp lại trong khi, trong khi, làm trong khi, cho và cho các trò chơi trong. Introduction JavaScript provides three types of selection structures.
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. The if…else [double-selection statement] selection statement performs an action if a condition is true and performs a different action if the
condition is false. The switch [multiple-selection statement] selection statement performs one of many different actions, depending on the value of an expression. JavaScript provides four repetition statements—while, do…while, for and for…in.
3
4 Nếu Tuyên bố lựa chọn Tuyên bố lựa chọn được sử dụng để chọn trong số các khóa học hành động thay thế trong một kịch bản. Ví dụ: mã giả trước đó nếu câu lệnh có thể được viết bằng JavaScript AS 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
5 Nếu câu lệnh lựa chọn khác, nếu câu lệnh Lựa chọn khác cho phép bạn chỉ định rằng một hành động khác sẽ được thực hiện khi điều kiện là đúng so với điều kiện là sai. Ví dụ: mã giả trước đó nếu câu lệnh khác có thể được viết bằng JavaScript AS if…else Selection Statement
The if…else 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 If…Else statement may be written in JavaScript as
6 toán tử có điều kiện [? :] Toán tử?: Toán tử ternary duy nhất của JavaScript có thể thực hiện ba toán hạng. Biểu thức nếu biểu thức đánh giá là TRUE và thứ ba là giá trị cho biểu thức có điều kiện nếu biểu thức đánh giá là sai. Conditional Operator [?:]
The operator ?: is JavaScript’s only ternary operator—it 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.
7 lồng nhau nếu ... các câu lệnh khác nếu các câu lệnh khác kiểm tra cho nhiều trường hợp bằng cách đặt nếu các câu lệnh khác bên trong nếu các câu lệnh khác. Nested if...else Statements
Nested if…else statements test for multiple cases by placing if…else statements inside if…else statements.
8 Vấn đề lủng lẳng-đòi hỏi sau đây minh họa vấn đề lơ lửng. Ví dụ: trình thông dịch diễn giải câu lệnh trước đó buộc câu lệnh nested đầu tiên phải thực thi vì dự định ban đầu, chúng ta phải viết nó như sau: niềng răng phần thân của câu lệnh IF đầu tiên và cái khác được khớp với câu lệnh IF đầu tiên. 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.
9 Trong khi cấu trúc lặp lại của Statementa Repetition [còn được gọi là vòng lặp] cho phép bạn chỉ định rằng tập lệnh là lặp lại một hành động trong khi một số điều kiện vẫn đúng. Câu lệnh như sau: 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:
10 Toán tử gán JOT 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 %
11 toán tử tăng và giảm Increment and Decrement Operators
12
13 Thuật toán xây dựng: RepetitionConsider kiểm soát đối phó Tuyên bố vấn đề sau: Formulating Algorithms: Counter-Controlled Repetition
Consider the following problem statement:
14
15 Các thuật toán xây dựng: RepetitionConsider kiểm soát Sentinel Vấn đề sau: Phát triển tập lệnh trung bình lớp sẽ xử lý số lượng điểm tùy ý mỗi khi tập lệnh được chạy. 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.
16
17 câu lệnh kiểm soát lồng nhau Nested Control Statements
18
19
20 yếu tố cần thiết của sự lặp lại được kiểm soát phản hồi được kiểm soát theo yêu cầu: Tên của biến điều khiển [hoặc bộ đếm vòng lặp]. Giá trị ban đầu của biến điều khiển. Tăng [hoặc giảm] theo đó biến điều khiển được sửa đổi mỗi lần [còn được gọi là mỗi lần lặp của vòng lặp]. Điều kiện kiểm tra giá trị cuối cùng của biến điều khiển để xác định vòng lặp nên tiếp tục. Essentials of Counter-Controlled Repetition
Counter-controlled repetition requires: The name of a control variable [or loop counter].
The initial value of the control variable. The increment [or decrement] by which the control variable is modified each time through the loop [also known as each iteration of the loop]. The condition that tests for the final value of the control variable to determine looping should continue.
21 cho tuyên bố lặp lại for Repetition Statement
22 Định dạng chung của một tuyên bố General Format of a for Statement
23 Số nguyên tổng hợp với một tuyên bố
Summing Integers with a for Statement
24 Tính lãi kép với for for statementconsider Tuyên bố vấn đề sau: Calculating Compound Interest with the for Statement
Consider the following problem statement:
25
26 Chuyển đổi tuyên bố nhiều lựa chọn switch Multiple-Selection Statement
27
28 Chúng tôi tạo các danh sách khác nhau này bằng cách sử dụng kiểu danh sách thuộc tính CSS, cho phép chúng tôi đặt hệ thống đánh số cho danh sách. Các giá trị có thể La Mã [chữ số La Mã chữ hoa], alpha thấp hơn [chữ thường], trên alpha [chữ hoa], v.v. 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 [numbers—the default], lower-roman [lowercase Roman numerals], upper-roman [uppercase Roman numerals], lower-alpha [lowercase letters], upper-alpha [uppercase letters], and more.
29 Làm trong khi câu lệnh lặp lại trong khi câu lệnh, thử nghiệm tiếp tục vòng lặp xảy ra khi bắt đầu vòng lặp, trước khi phần thân của vòng lặp thực thi. Cơ thể vòng lặp luôn thực hiện ít nhất một lần. do…while 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 do…while statement tests the loop-continuation condition after the loop body executes—therefore, the loop body always executes at least once.
30
31 Break và tiếp tục Tuyên bố Tuyên bố Breakthe Break Statement, khi được thực thi trong một thời gian, vì, thực hiện trong khi hoặc câu lệnh chuyển đổi, gây ra thoát ngay lập tức khỏi câu lệnh. break and continue Statements
break Statement The break statement, when executed in a while, for, do…while or switch statement, causes immediate exit from the statement. Execution continues with the first statement after the structure.
32 Tiếp tục tuyên bố Tuyên bố tiếp tục, khi được thực thi trong một thời gian, vì hoặc thực hiện trong khi tuyên bố, bỏ qua các câu lệnh còn lại trong phần thân của câu lệnh và tiến hành lần lặp tiếp theo của vòng lặp. Kiểm tra tiếp tục đánh giá ngay sau khi câu lệnh tiếp tục thực thi. Trong các câu lệnh, biểu thức gia tăng thực thi, sau đó thử nghiệm tiếp tục vòng lặp đánh giá. Vị trí tiếp tục tiếp tục trước khi tăng trong một thời gian có thể dẫn đến một vòng lặp vô hạn. continue Statement The continue statement, when executed in a while, for or do…while statement, skips the remaining statements in the body of the statement and proceeds with the next iteration of the loop. In while and do…while statements, the loop-continuation 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.
33 Toán tử logicJavaScript cung cấp các toán tử logic có thể được sử dụng để hình thành các điều kiện phức tạp hơn bằng cách kết hợp các điều kiện đơn giản. Các toán tử logic là && [logic và], || [logic hoặc] và! [logic không, còn được gọi là phủ định logic]. && [[logic và] toán tử 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
34 || [Logic hoặc] toán tử! [Phủ định logic] Nhà điều hành || [Logical OR] Operator
! [Logical Negation] Operator