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.
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.
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.