intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Loop Exceptions

Chia sẻ: Qweqwdasd Qweqdasda | Ngày: | Loại File: PDF | Số trang:8

66
lượt xem
3
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Vòng ngoại lệ Nói chung, một vòng lặp tiếp tục thực hiện lặp lại cho đến khi tình trạng của nó là không còn đúng nữa. Bạn sử dụng hai hành động để thay đổi hành vi này: tiếp tục và phá vỡ. Với việc tiếp tục hành động, bạn có thể ngừng lặp đi lặp lại hiện tại (có nghĩa là, không có hành động hơn nữa trong sự lặp lại mà sẽ được thực hiện) và chuyển thẳng đến phiên bản tiếp theo trong vòng một....

Chủ đề:
Lưu

Nội dung Text: Loop Exceptions

  1. < Day Day Up > Loop Exceptions In general, a loop continues to perform iterations until its condition is no longer true. You use two actions to change this behavior: continue and break. With the continue action, you can stop the current iteration (that is, no further actions in that iteration will be executed) and jump straight to the next iteration in a loop. For example: var total:Number = 0; var i:Number = 0; while (++i
  2. remains true. For example: var total:Number = 0; var i:Number = 0; while (++i = 10) { break; } } This script increases the value of a variable named total by 1 with each iteration. When the value of total is 10 or greater—as checked by an if statement—a break action occurs and the while statement halts, even though it's set to loop 20 times. In the following exercise, you'll use continue and break in a simple search routine. 1. Open phoneNumberSearch1.fla in the Lesson09/Assets folder. This file contains two layers: Actions and Search Assets. The Actions layer will contain the search routine for this project. The Search Assets layer contains the text fields, button, and graphics for this exercise.
  3. In this exercise, you'll produce a simple application that lets you enter a name in a search field to return the phone number for that individual. Two text fields are on the screen: name_txt will be used to enter the name to search; result_txt will display the search result. An invisible button over the Search button graphic has an instance name of search_btn that will be used to call a search function. 2. With the Actions panel open, select the first frame in the Actions layer and add the following script: 3. 4. var info:Array = [["John","919-555-5698"],["Kelly","232-555-3333"], ["Ross","434-555-5655"]]; 5. This script creates a two-dimensional array called info containing three elements, each of which is its own array, or sub-array. The first element of each sub-array is a name, and the second element of each sub-array is a phone number.
  4. To access the first name in this array, you use info[0][0]; the first phone number would be accessed using info[0][1]. This syntax represents John's name and number. The syntax will play an important role in the search function we're about to script. 3. Add this function definition after the info array: 4. 5. function search () { 6. 7. var matchFound:Boolean = false; 8. 9. var i:Number = -1; 10. 11. while (++i < info.length) { 12. 13. } 14. 15. } 16. You've begun to define the function that will search the info array for a specific phone number. The first action in this function creates a variable called matchFound and assigns it an initial value of false. (We'll soon show you how this variable will be used.) We've set up the while statement to loop once for every element in the info array. 4. Add this action to the while loop in the search() function: 5. 6. if (info[i][0].toLowerCase() != name_txt.text.toLowerCase()) {
  5. 7. 8. continue; 9. 10. } 11. 12. result_txt.text = info[i][1]; 13. 14. matchFound = true; 15. 16. break; 17. With each iteration of the loop, the if statement uses the current value of i to determine whether mismatches exist between names (made all lowercase) in the info array and the user-entered name (also forced to lowercase) in the name_txt text field. When a mismatch is encountered, the continue action within the if statement is evoked and the script skips to the next loop. Using toLowerCase() to convert names to lowercase makes the search case insensitive. If a name in the info array matches one in the name_txt text field, continue is not invoked and the actions after the if statement are executed. Using the value of i at the time a match was found, the first action sets the value of the variable result_txt.text to the matching phone number, sets matchFound to true, and executes the break action to halt the loop. To understand better how this action works, imagine that someone has entered Kelly into the name_txt text field. The location of Kelly in the info array is as follows: info[1][0] On the first iteration of the loop, the value of i is 0, which means that the if statement in the loop would look like this:
  6. if (info[0][0].toLowerCase() != name_txt.text.toLowerCase()) { continue; } Here the statement asks whether john (the name at info[0][0], made lowercase) is not equal to kelly (entered into the name_txt text field and made lowercase). Because john does not equal kelly, the continue action is invoked and the next loop begins. Because i is incremented by 1 with each loop, on the next iteration the if statement looks like this: if (info[1][0].toLowerCase() != name_txt.text.toLowerCase()) { continue; } Here the statement asks whether kelly (the name at info[1][0], made lowercase) is not equal to kelly (entered into the name_txt text field and made lowercase). Because kelly does equal kelly, the continue action is skipped and the next three actions are executed. The first action sets the value of result_txt.text to info[i][1]. Because i has a value of 1 when this action is executed, result_txt.text is set to info[1][1]—Kelly's phone number, which is now displayed. Next, matchFound is set to true and the break action exits the loop. NOTE Although the break action is not a necessity, it helps shorten search times. Think of how much time you would save by using a break action to avoid unnecessary loops in a 10,000-name array! 5. Add this if statement as the last action in the search() function: 6. 7. if (!matchFound) { 8. 9. result_txt.text = "No Match";
  7. 10. 11. } 12. This statement, which is not part of the loop, checks whether matchFound still has a value of false (as it was set initially) once the loop is complete. If it does, the action in the if statement sets the value of result_txt.text to No Match. The syntax !matchFound is shorthand for matchFound == false. 6. Add this script at the end of the frame, after the search() function: 7. 8. search_btn.onRelease = function() { 9. 10. search(); 11. 12. }; 13. The script adds an onRelease event handler to the search_btn button. When the Search button is released, the search() function is executed.
  8. 7. Chose Control > Test Movie. Enter John, Kelly, or Ross in the search field and click the Search button. Then enter any other name and click the Search button. A phone number should appear when a name is valid, and "No Match" should appear when no name is contained within the info array. 8. Close the test movie and save your work as phoneNumberSearch2.fla. In this exercise, you used the loop exceptions continue and break to create a search routine. In practice, the break command is used more often than the continue command; this is because programmers often use if statements rather than continue to bypass actions in a loop. < Day Day Up >
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2