Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repeating Blocks: Iteration 靜宜大學資管系 楊子青

Similar presentations


Presentation on theme: "Repeating Blocks: Iteration 靜宜大學資管系 楊子青"— Presentation transcript:

1 Repeating Blocks: Iteration 靜宜大學資管系 楊子青 2013.3.18.

2 Iteration One thing computers are good at is repeating operations.
In this chapter, you’ll learn how to program repetition with just a few blocks instead of copying and pasting the same blocks over and over. repeat blocks can significantly simplify an app.

3 Two blocks for repeating operations
The foreach block applies a set of functions to each element of a list. While is more general: you can use it to process a list. With while, the inner blocks are performed continuously while a certain condition is true. After the blocks within the while are executed, control loops back up and the test condition is tried again. Only when the test evaluates to false does the while block complete.

4 1. Foreach Exampe: Copying and pasting the blocks for each phone number to be texted

5 Foreach Repeating Functions on a List Using foreach
This code can be read as: For each item (phone number) in the list phoneNumbers, set the Texting object’s phone number to the item and send out the text message.

6 A Second foreach Example: Displaying a List
The simple way to display a list is to plug it directly into a label the list items are displayed in the label as a single row: (111– – –3333) we want a newline character: \n. “\n111–1111\n222–2222\n333–3333” would appear as:

7 Using the foreach block to process the list and put a newline character
A closer Look At Looping:

8 練習:資管系實驗室列表App 延續上一章練習: 將實驗室顯示於Label (每筆資料跳行顯示)
讓使用者利用Textbox輸入資管系實驗室,將以下資料加入List變數,於ListPicker顯示結果 主顧403 電子商務實驗室 (403EC) 主顧405 企業資源規劃實驗室 (405ERP) 主顧407 醫療資訊系統實驗室 (407HIS) 主顧409 資料庫與資料探勘實驗室 (409DB) 使用者如果在ListPicker選了一個實驗室,則將該實驗室資料刪除 將實驗室顯示於Label (每筆資料跳行顯示)

9 2. While The while block is a bit more complicated to use than foreach. foreach repeats over a list, but while can repeat while any arbitrary condition (任意條件) is true.

10 Using while to Compute a Formula
假設使用者輸入3: N Number total 3 1 2 4 6

11 Using while to Process Lists

12 練習:資管系實驗室列表App 利用While取代foreach,改寫本章練習


Download ppt "Repeating Blocks: Iteration 靜宜大學資管系 楊子青"

Similar presentations


Ads by Google