Loops, Databases, Procedures, and Lists Dr. José M. Reyes Álamo.

Post on 03-Jan-2016

219 views 0 download

transcript

Loops, Databases, Procedures, and Lists

Dr. José M. Reyes Álamo

2

Outline

Loops

Databases

Procedures

Lists

Loops

4

Some times you need to repeat…

While the test is TRUE do

5

6

Some times you need to repeat…

For each element in the list do

7

8

Some times you need to repeat…

For each value of i in the range of start to end on steps of step do

9

Databases

11

Permanent Storage

• TinyDB – Persistent storage in your device

• TinyWebDB– Persistent storage in the Web

12

TinyDB

Item (tag) Quantity (valueToStore)

Pasta 1 Box

Sauce 1 Jar

Grocery List

13

14

Procedures

16

What is a procedure?

A procedure is a named sequence of blocks that encapsulate some functionality and can be called from any place in your app.

17

More about procedures

• Procedures are an abstraction since they represent a recognizable name for a sequence of lower-lever instructions.

• In other programming languages procedures are called functions or subprograms.

18

Why are procedures useful?

• Procedures can be a good tool to develop large apps.

– You create new functionality by using procedures.

– You can use them to break down a large problem or project into more manageable sub problems.

– While designing the app, you can define a procedure ignoring the details of how it works and refer to the empty procedure. When your finish your design, you can add code to the empty procedures to complete the functionality of your app.

– You can put chunks of code in one place and call it from various places in your app.

19

Two blocks to define procedures

• The procedure block collects a sequence of blocks together. – When you create a procedure, AppInventor automatically generates a call

block and places it in the My Definitions drawer.

– You use the call block to invoke the procedure.

• The procedureWithResults block works similarly to procedure but also returns a result. – After the procedure executes, the result is returned to the block

connected to the return socket.

20

Refactoring

21

Refactoring

• Refactoring is programming technique that consist on reorganizing the code, for example create a procedure to substitute redundant code, to make it easier to maintain and easier for the programmers to work with.

Lists

23

What is a list?

• They are variables that instead of holding a single value, they hold several values distinguished by an index.

• Simple variables– score = 100

– phoneNumber = 201-122-4322

• Lists– bestScores = [100, 98, 89]

– myContactsPhoneNumbers =

[201-122-4322, 803-234-3293, 977-121-5383]

24

What is a list?

• Simple variables– score = 100

– phoneNumber = 201-122-4322

• Lists– bestScores = [100, 98, 89]

– myContactsPhoneNumbers =

[201-122-4322,

803-234-3293,

977-121-5383]

25

List operations

26

OpenLab and Blackboard

• Check OpenLab for any new lab.

• Check Blackboard for any new quiz.

• Project:

– Brainstorm an idea

– Get professor's approval

– Post your idea in your portfolio

– Must work in teams of 2 or 3

– It must be an app and the topic is open: a game, a tool, an enhanced version of a previous app

27

OpenLab and Blackboard

• Finish your labs and post them in OpenLab.

• Finish quizzes on Blackboard.

• Work on your project the next few weeks.

• Study for the Final Exam.