Nội dung bài họcLesson content
Điều kiện là gì?What is a condition?
Điều kiện là cách bảo máy tính: "NẾU điều này đúng, THÌ làm việc A, NẾU SAI, THÌ làm việc B". Ví dụ: NẾU đèn xanh THÌ đi, NẾU đèn đỏ THÌ dừng. Máy tính kiểm tra điều kiện và quyết định hành động.A condition is a way to tell the computer: "IF this is true, THEN do action A, IF false, THEN do action B." For example: IF green light THEN go, IF red light THEN stop. The computer checks the condition and decides what to do.
Khối if trong ScratchThe if block in Scratch
Khối "if <điều kiện> then" sẽ chạy các khối bên trong chỉ khi điều kiện đúng. Khối "if <điều kiện> then else" cho phép làm việc khác khi điều kiện sai. Điều kiện thường dùng khối sensing như "touching color" hoặc "key pressed".The "if <condition> then" block runs the blocks inside only when the condition is true. The "if <condition> then else" block lets you do something else when the condition is false. Conditions often use sensing blocks like "touching color" or "key pressed".
when flag clicked
if <touching color red?> then
say "Đèn đỏ! Dừng lại!" for 2 seconds
else
say "Đi tiếp!" for 2 seconds
Game đèn giao thôngTraffic light game
Tạo game: sprite xe sẽ dừng khi chạm màu đỏ và đi khi chạm màu xanh. Dùng backdrop có đèn giao thông. Lập trình: if touching red → say "Dừng!", else → say "Đi!". Thay đổi màu đèn và xem xe phản ứng.Create a game: the car sprite stops when touching red and goes when touching green. Use a backdrop with a traffic light. Program: if touching red → say "Stop!", else → say "Go!". Change the light color and watch the car react.
Nhiều điều kiện với else ifMultiple conditions with else if
Trong Scratch, bạn có thể xếp chồng nhiều khối if để kiểm tra nhiều điều kiện. Ví dụ: if touching red → dừng, if touching yellow → chậm lại, if touching green → đi. Máy tính kiểm tra từng điều kiện từ trên xuống.In Scratch, you can stack multiple if blocks to check multiple conditions. For example: if touching red → stop, if touching yellow → slow down, if touching green → go. The computer checks each condition from top to bottom.