Cấp độ: beginner20 phútminMiễn phíFreeCreative ExplorerCreative Explorer

Vòng lặp và sự lặp lạiLoops and repetition

Họa tiết hình học bằng ScratchA geometric pattern in Scratch

Bạn sẽ học gìWhat you will learn

  • Hiểu khái niệm vòng lặpUnderstand the concept of loops
  • Dùng khối repeat trong ScratchUse the repeat block in Scratch
  • Vẽ hình bằng penDraw shapes with pen

Bạn sẽ tạo gìWhat you will build

Họa tiết hình học bằng ScratchA geometric pattern in Scratch

Nội dung bài họcLesson content

Vòng lặp là gì?What is a loop?

Vòng lặp (loop) là cách bảo máy tính làm đi làm lại một việc nhiều lần. Thay vì viết "move 100 steps, turn 60 degrees" 6 lần, bạn chỉ cần viết 1 lần và đặt trong khối "repeat 6". Vòng lặp giúp code ngắn hơn và dễ sửa hơn.A loop is a way to tell the computer to do something again and again many times. Instead of writing "move 100 steps, turn 60 degrees" 6 times, you write it once and put it inside a "repeat 6" block. Loops make code shorter and easier to fix.

Khối repeat trong ScratchThe repeat block in Scratch

Khối "repeat [n]" lặp lại tất cả khối bên trong nó n lần. Ví dụ: repeat 4 sẽ làm 4 lần. Bạn có thể đổi số n thành bất kỳ số nào. Mọi khối nằm giữa repeat và end sẽ được lặp lại.The "repeat [n]" block repeats all blocks inside it n times. For example: repeat 4 will run 4 times. You can change n to any number. All blocks between repeat and its end will be repeated.

scratch
                      
                        when flag clicked
repeat 6
  move 100 steps
  turn 60 degrees
                      
                    

Vẽ hình với PenDrawing shapes with Pen

Scratch có khối Pen để sprite vẽ đường đi. Dùng "pen down" để bắt đầu vẽ, "pen up" để ngừng vẽ. Khi sprite di chuyển, nó sẽ vẽ đường. Thử vẽ hình lục giác: repeat 6, move 100 steps, turn 60 degrees.Scratch has Pen blocks that let the sprite draw a trail. Use "pen down" to start drawing, "pen up" to stop. When the sprite moves, it draws a line. Try drawing a hexagon: repeat 6, move 100 steps, turn 60 degrees.

scratch
                      
                        when flag clicked
pen down
repeat 6
  move 100 steps
  turn 60 degrees
pen up
                      
                    

Tại sao vòng lặp tiết kiệm thời gian?Why do loops save time?

Nếu muốn vẽ hình 12 cạnh, bạn cần lặp 12 lần. Không có vòng lặp, bạn phải viết 24 khối (12 move + 12 turn). Có vòng lặp, bạn chỉ cần 2 khối bên trong repeat 12. Nếu muốn đổi từ 12 thành 20 cạnh, chỉ cần đổi 1 số.If you want to draw a 12-sided shape, you need to repeat 12 times. Without a loop, you must write 24 blocks (12 move + 12 turn). With a loop, you only need 2 blocks inside repeat 12. If you want to change from 12 to 20 sides, just change one number.

Bài tập thực hànhHands-on exercises

1Dùng repeat để vẽ hình tam giác (3 cạnh, xoay 120 độ mỗi lần).Use repeat to draw a triangle (3 sides, turn 120 degrees each time).

Gợi ýHint

Tổng góc xoay = 360 độ. 360 / 3 = 120 độ.Total turn angle = 360 degrees. 360 / 3 = 120 degrees.

Lời giảiSolution
scratch
                      
                        when flag clicked
pen down
repeat 3
  move 100 steps
  turn 120 degrees
pen up
                      
                    

2Vẽ hình sao 5 cánh bằng cách lặp 5 lần: move 100, turn 144 độ.Draw a 5-pointed star by repeating 5 times: move 100, turn 144 degrees.

Gợi ýHint

Góc xoay cho sao 5 cánh là 144 độ, không phải 72 độ.The turn angle for a 5-pointed star is 144 degrees, not 72 degrees.

Lời giảiSolution
scratch
                      
                        when flag clicked
pen down
repeat 5
  move 100 steps
  turn 144 degrees
pen up
                      
                    

Kiểm traQuiz

Chọn đáp án đúng cho mỗi câu. Nhấn để xem giải thích.Choose the correct answer for each question. Click to see explanation.

Câu 1: Vòng lặp giúp gì?Q1: What do loops help with?

Câu 2: Để vẽ hình vuông, mỗi lần xoay bao nhiêu độ?Q2: To draw a square, how many degrees do you turn each time?

Câu 3: Khối "pen down" làm gì?Q3: What does the "pen down" block do?

Code mẫuSample code

scratch
                when flag clicked
repeat 6
  move 100 steps
  turn 60 degrees