Writing and Understanding Loop Conditions

Các điều kiện trong vòng lặp là i | Writing and Understanding Loop Conditions For the rest of this lesson we ll focus on the while loop. The actions within this type of loop are performed continuously as long as the condition evaluates to true. For example var i Number 0 while i 10 perform these actions The condition in the loop is i 10. This means that as long as the value of i is less than 10 the statement is true and the actions within the loop are repeated. However the looping statement is missing a key ingredient it doesn t have a means by which the condition eventually becomes false. Without this functionality the loop could continue forever and in the real world endless loops cause applications to freeze. Flash can t do anything else until the looping statement completes its job. To prevent an endless loop in the example the value of i must be incremented so that its value eventually equals 10 at which point the condition proves false and the loop stops. You can use the increment operator to handle incrementing a loop. Here s an example var i Number 0 while i 10 perform these actions i Incrementing the value of i causes the loop to perform 10 iterations. The value of i is initially set to 0. However with each loop that value increases by 1. On the tenth loop i 10 which means that i 10 is no longer true and the loop halts. Here s a shortcut that accomplishes the same goal var i Number 0 while i 10 perform these actions This loop performs 9 iterations. The value of i is initially set to 0. However with each iteration including the first that value is incremented by 1 within the conditional statement of the loop itself. On the tenth loop i 10 which means that i 10 is no longer true and the loop halts. You can also use the decrement operator with a loop which might look something like this var i Number 10 while --i 0 perform these actions Alternatively you can write this script using a for loop as follows for var i Number 10 i 0 --i

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.