Download presentation
Presentation is loading. Please wait.
Published byJurgen van der Pol Modified over 6 years ago
1
Programming Your App to Make Decisions – Conditional Blocks
App Inventor – Chapter 18 Programming Your App to Make Decisions – Conditional Blocks
2
From Chapter 18 Introduction
Computers, even small ones like the phone in your pocket, are good at performing thousands of operations in just a few seconds. Even more impressively, they can also make decisions based on the data in their memory banks and logic specified by the programmer. This decision-making capability is probably the key ingredient of what people think of as artificial intelligence, and it’s definitely a very important part of creating smart, interesting apps! In this chapter, we’ll explore how to build decision-making logic into your apps.
3
Decisions, decisions, decisions . . .
Computer code (no matter the language) executes these three ways: Sequentially Using conditional logic Repetitively The ‘normal’ assumption with code is that it executes sequentially Here we explore decisions . . . In most languages, there are two ways to express conditional logic: if-then-else (practically all languages) switch (C++, Java) or Select-Case (Visual Basic)
4
Look at this event handler:
When Event1 is triggered, block A is executed Then some kind of condition is tested If true, block B1 is executed (B1 ‘branch’). If not, block B2 is executed (B2 ‘branch’). Block C is executed either way What are the possible paths through the code?
5
if and if-else blocks in App Inventor
6
‘if’ and ‘if-else’ are used with ‘relational operator’ and/or ‘logical operator’ blocks:
Relational Operators Logical Operators These get plugged into the test blocks of ‘if’ and ‘if-else’ blocks
7
Example of ‘if-then’: What happens if ‘score’ is greater than 100?
What happens if ‘score’ is less than 100? What happens if ‘score’ is exactly equal to 100?
8
Programming an either/or situation
9
Conditions within conditions
10
Complex conditions Example: Is your phone somewhere inside the Harney Science Center at University of San Francisco? Is the phone’s latitude less than the maximum latitude ( ) of the boundary? Is the phone’s longitude less than the maximum longitude (– ) of the boundary? Is the phone’s latitude more than the minimum latitude ( ) of the boundary? Is the phone’s longitude more than the minimum longitude (– ) of the boundary?
11
Here are the blocks you’ll need:
12
Here are the blocks you’ll need:
13
But we can handle even more complexity . . .
Suppose you wanted the phone to vibrate only when the boundary was crossed from inside to outside You’ll need one of these:
14
There’s a lot of subtle stuff going on here. Check it out carefully.
15
Same or different?
16
Summary Is your head spinning? That last behavior was quite complex! But it’s the type of decision making that sophisticated apps need to perform. If you build such behaviors part by part (or branch by branch) and test as you go, you’ll find that specifying complex logic--even, dare we say, artificial intelligence--is doable. It will make your head hurt and exercise the logical side of your brain quite a bit, but it can also be lots of fun.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.