Lựa chọn và Lặp lại
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| selection/sɪˈlekʃn/ | Lựa chọn |
| iteration/ˌɪtəˈreɪʃn/ | Lặp lại |
Beyond straight-line code
- So far, code has run top to bottom, one statement after another.
- Real algorithms need to choose and to repeat — not just run in a straight line.
- Selection 选择: pick which code to run based on a condition (
if). - Repetition (iteration 迭代): run some code many times (
while,for).
Selection: making decisions
- Selection lets a program take different paths depending on the data.
- "If the score is above $90$, print an A; otherwise print a B."
- The program branches — only one path runs each time.
- Unit 2 builds this with Boolean conditions and
ifstatements.

The three control structures: sequence, selection, and iteration
Repetition: doing it again
- Repetition runs a block of code over and over until a condition says stop.
- "Keep asking for a password until the user types the right one."
- A loop saves you from copying the same code many times.
- Loops are how programs process lists, totals, and searches.
Combining the three
- The three building blocks of every algorithm: sequence, selection, repetition.
- Sequence = do steps in order; selection = choose; repetition = repeat.
- Almost every program is these three, nested and combined.
- Mastering them lets you express any computation.
Theo dõi vòng lặp
Đi qua một vòng lặp đếm và theo dõi tổng tích lũy dần lên.
Selection and repetition sound similar but do different jobs. Selection (if) chooses whether a block runs once based on a condition; repetition (a loop) runs a block many times while a condition holds. A program combines sequence + selection + repetition — these three building blocks express every algorithm.
An algorithm for grading:
- Sequence: read the score.
- Selection: if
score >= 90, it's an A; else a B. - Repetition: do this for every student in the list.
Algorithms need more than sequence — they need selection (choose a path with if, based on a Boolean condition) and repetition/iteration (repeat a block with a loop). These three building blocks — sequence, selection, repetition — combine and nest to express any algorithm.
Việc chọn mã cần chạy dựa trên một điều kiện được gọi là...
Độ 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
Lựa chọn (if) chọn một đường đi; lặp lại (vòng lặp) thực thi nhiều lần một khối lệnh.
Thực thi nhiều lần một khối mã được gọi là...
Độ 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
Lặp lại/lặp được thực hiện bằng các vòng lặp while và for.
Ba khối xây dựng cơ bản của thuật toán là gì?
Độ 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ãy, lựa chọn và lặp lại biểu diễn mọi thuật toán.
Câu lệnh if quyết định xem một khối có chạy hay không, trong khi vòng lặp có thể chạy một khối nhiều lần.
Độ 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
Lựa chọn = chọn một lần; Lặp lại = lặp lại.
Chạy mã nhiều lần với vòng lặp được gọi là ___ (một từ, đồng nghĩa với lặp lại).
Độ 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
Iteration là sự lặp lại thông qua các vòng lặp.