Biểu thức Toán học
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| arithmetic/əˈrɪθmətɪk/ | Toán học |
| arithmetic expression/əˈrɪθmətɪk ekˈspreʃn/ | Biểu thức toán học |
| order of operations/ˈɔːdə ɒv ˌɒpəˈreɪʃnz/ | Thứ tự thực hiện phép tính |
| remainder/rɪˈmeɪndə/ | Phần dư |
| modulus/ˈmɒdjʊləs/ | Lấy phần dư (modulus) |
| integer/ˈɪntɪdʒə/ | Số nguyên |
Doing arithmetic in a program
- Programs often do arithmetic 算术.
- An arithmetic expression 算术表达式 combines numbers with operators: $+$, $-$, $\times$, $\div$.
- The result is usually stored in a variable for later, e.g. $\text{area} \leftarrow \text{length} \times \text{width}$.
- To get the right answer, you must apply the operators in the right order.
Áp dụng thứ tự thực hiện phép tính
Nhân và chia có độ ưu tiên cao hơn cộng và trừ, và các toán tử cùng cấp độ thì đi từ trái sang phải. Tính toán từng bước một.
2 + 3 × 4 bằng bao nhiêu?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Nhân trước: 3 × 4 = 12, sau đó 2 + 12 = 14.
(2 + 3) × 4 bằng bao nhiêu?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Dấu ngoặc trước: 2 + 3 = 5, sau đó 5 × 4 = 20.
Order of operations
- A computer follows the order of operations 运算顺序 — the same rules as maths.
- Do multiplication and division before addition and subtraction.
- Work left to right for operators of equal rank; do anything in brackets first.
- So $2 + 3 \times 4 = 14$ (not 20); writing $(2 + 3) \times 4$ forces the addition first, giving 20.

MOD gives the remainder 余数; multiplication comes before addition
17 MOD 5 (số dư) là bao nhiêu?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
17 ÷ 5 là 3 dư 2, nên 17 MOD 5 = 2.
Làm thế nào để chương trình kiểm tra xem số n có chẵn không?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Một số chẵn không để lại số dư khi chia cho 2.
The modulus operator
- The modulus 取模 operator (
MOD) returns the remainder of integer division. - For example $17 \bmod 5 = 2$, because $17 \div 5$ is 3 with 2 left over.
- Modulus is handy for tests: a number is even when $n \bmod 2 = 0$.
- It comes up constantly — cycling through positions, checking divisibility, and more.
Tính 10 + 6 ÷ 2 × 3.
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
6 ÷ 2 = 3, sau đó 3 × 3 = 9, cuối cùng 10 + 9 = 19.
Chia hai số nguyên có thể cho ra số thập phân, ví dụ 7 ÷ 2 = 3.5.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Một số ngôn ngữ trả về 3.5; một số khác thực hiện chia số nguyên và trả về 3.
Integer vs decimal
- Think about integer 整数 versus decimal results.
- Adding two integers gives an integer, but division can give a decimal: $7 \div 2 = 3.5$.
- Some languages treat $7 \div 2$ as integer division and give $3$ — know which you get.
Evaluate $10 + 6 \div 2 \times 3$. Division and multiplication first, left to right: $6 \div 2 = 3$, then $3 \times 3 = 9$. Now add: $10 + 9 = 19$.
An arithmetic expression must follow the order of operations: brackets, then $\times \div$, then $+ -$, left to right — so $2 + 3 \times 4 = 14$. The modulus operator gives the remainder ($17 \bmod 5 = 2$), great for even/odd tests. Watch integer vs decimal division ($7 \div 2 = 3.5$ or $3$).