Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Your App to Make Decisions: Conditional Blocks 靜宜大學資管系 楊子青

Similar presentations


Presentation on theme: "Programming Your App to Make Decisions: Conditional Blocks 靜宜大學資管系 楊子青"— Presentation transcript:

1 Programming Your App to Make Decisions: Conditional Blocks 靜宜大學資管系 楊子青 2013.3.11.

2 Decision-making capability
is the key ingredient (關鍵因素) of what people think of as artificial intelligence In this chapter, we’ll explore how to build decision-making logic into your apps.

3 An event handler that tests for a condition and branches accordingly
When the event1 occurs, function A is performed no matter what. Then a decision test is performed. If the test is true, B1 is performed. If it is false, B2 is performed. In either case, the rest of the event handler (C) is completed.

4 Testing Conditions with if and ifelse Blocks
App Inventor provides two types of conditional blocks: if and ifelse, both of which are found in the Control drawer of the Built-In palette.

5 Relational and logical operator blocks used in conditional tests
You can plug any Boolean expression into the “test” slot of these blocks. A Boolean expression is a mathematical equation that returns a result of either true or false.

6 Example: This ifelse block calls one of two numbers based on the randomly generated integer

7 Example: An ifelse condition is placed within the else-do of an outer condition
Placing one control construct within another is called nesting. In this case, you’d say the blocks had a “nested if-else.”

8 Programming Complex Conditions
You can build complex tests using the logical operators and, or, and not, which are found in the Logic drawer.

9 Can you tell whether these two if tests are equivalent?
The answer is “yes!” The test on the left compares the value of a Boolean variable with true. If withinBoundary contains true, you compare true to true, which is true. If the variable contains false, you compare false to true, which is false. The test on the right is easier to code.

10 練習:請設計一個擲筊的App 擲筊(擲爻) 凸起部份稱為「陰面」(也就是反面),平面的部份則稱為「陽面」(也就是正面)。 會出現三種情況:
聖杯:一正一反,表示神明同意你的請求 笑杯:兩個平面向上,表示神明在笑,還未決定同不同意 蓋杯(陰杯):兩個平面朝下,表示請求駁回

11 提示 拉一個Button,讓使用者開始擲筊 設定變數Divination1, Divination2,以及Rnd
假設1表示正面:Divination變數值設為True 假設2表示背面:Divination變數值設為False 先用一個Label,暫時顯示兩個筊杯的數字 根據Divination1, Divination2判斷擲筊結果,於手機螢幕顯示出相關的圖片

12 執行實例


Download ppt "Programming Your App to Make Decisions: Conditional Blocks 靜宜大學資管系 楊子青"

Similar presentations


Ads by Google