22
EET 2259 Unit 7 Case Structures; Sequence Structures Read Bishop, Sections 5.4 and 5.5. Lab #7 and Homework #7 due next week. Quiz #3 next week.

EET 2259 Unit 7 Case Structures; Sequence Structures Read Bishop, Sections 5.4 and 5.5. Lab #7 and Homework #7 due next week. Quiz #3 next week

Embed Size (px)

Citation preview

Page 1: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

EET 2259 Unit 7Case Structures; Sequence Structures

Read Bishop, Sections 5.4 and 5.5.

Lab #7 and Homework #7 due next week.

Quiz #3 next week.

Page 2: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Review: Structures

Structures control the flow of a program’s execution.

We’ve looked at two kinds: For Loops While Loops

This week we’ll look at two more: Case Structures Sequence Structures

Page 3: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Case Structure

Case structures provide a way to execute different code depending on which one of several possible conditions is true.

Traditional programming languages accomplish the same thing using IF statements or CASE statements.

(Bishop, p. 236)

Page 4: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Placing a Case Structure

Case structures are found on the Programming>Structures palette.

As with loops, click it, and then drag to create a Case structure on the block diagram.

(Bishop, p. 236)

Page 5: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Subdiagrams

A Case structure has multiple subdiagrams, one of which is executed when the structure runs.

These subdiagrams are “stacked” on top of each other like a deck of cards, so that only one is visible at a time.

The selector label on the top border tells which case is visible, and has arrow buttons to let you step through the cases.

(Bishop, pp. 237)

Page 6: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Selector Terminal

A Case structure has one terminal, called the selector terminal.

This terminal, labeled ?, determines which one of the structure’s subdiagrams will be executed.

You’ll usually wire this terminal to a control or to the output of a function.

(Bishop, p. 238)

Page 7: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Selecting Which Case to Execute: Boolean Example

A Case structure with a Boolean selector terminal will have two subdiagrams: one that executes if the condition wired to the terminal is true, and one that executes if the condition is false.

Page 8: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Selecting Which Case to Execute: Numeric Example

A Case structure with a numeric selector terminal can have any number of subdiagrams: the one that’s executed depends on the value wired to the terminal.

Page 9: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Numeric Selector Labels

A numeric selector label can be: a single integer, such as 24 a list of integers, such as 1, 5, 11 a range of integers, such as

10..20 meaning all integers from 10 to 20 ..10 meaning all integers less than or = 10 10.. meaning all integers greater than or = 10

Selector labels cannot be floating point numbers.

(Bishop, p. 238)

Page 10: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Adding and Deleting Cases

Right-click a Case structure’s border and then choose Add Case Before or Add Case After

to add a case. choose Duplicate Case to copy a subdiagram

to a new case. choose Delete This Case to remove a case.

(Bishop, p. 239)

Page 11: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Default Case

Numeric case structures and string case structures usually have a default case, which will execute whenever none of the other cases apply.

To make a case the default case, right-click its border and choose Make This The Default Case.

(Bishop, p. 240)

Page 12: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Tunnels

Recall that a tunnel automatically appears on a structure’s border when you run a wire across the border.

If data flows from outside the structure to inside, the tunnel is an input tunnel.

If data flows from inside the structure to outside, it’s an output tunnel.

If an output tunnel is wired on one of a Case structure’s subdiagrams, then it must be wired on all of the subdiagrams.

(Bishop, pp. 240-241)

Page 13: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Example IF Statement in BASIC

CLS

INPUT “How many CDs are you buying? ”, CDs

IF CDs >= 5 THEN

Cost = 9 * CDs

PRINT “You get a discount!”

ELSE

Cost = 10 * CDs

END IF

PRINT “Total cost is $”; Cost

Page 14: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Example CASE Statement in BASIC

CLSINPUT “How many CDs are you buying? ”, CDsSELECT CASE CDsCASE IS >= 10

Cost = 8 * CDsCASE IS >= 5

Cost = 9 * CDsCASE ELSE

Cost = 10 * CDsEND SELECTPRINT “Total cost is $”; Cost

Page 15: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Order of Execution

In text-based programming languages, the order of the statements determines the order of execution.

In LabVIEW, if two functions are connected by wires, you can tell which one must execute first.

Page 16: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Sequence Structures

But in LabVIEW, you can’t always predict the order of execution. For example, in this code, which function executes first: the Subtract or the Greater Than?

Sequence structures can be used to force code to execute in a specific sequence.

(Bishop, p. 247)

Page 17: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Frames in Sequence Structures

The code to be executed sequentially is entered on subdiagrams called frames. The analogy is to frames of movie film, which are projected one after another.

(Bishop, p. 247)

Page 18: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Tick Count

The example on the previous slide used LabVIEW’s Tick Count function, which returns the number of milliseconds (ms) on your computer’s millisecond timer.

Generally this timer is reset to zero whenever the computer is rebooted.

Knowing that Tick Count uses the U32 datatype, let’s figure out how long it will take for it to “roll over” to zero again.

Page 19: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Tick Count Computations What is the largest integer we

can represent with U32? _____________ If that number is in ms, how

many seconds is that? ________________

How many minutes is that? ___________

How many hours is that? ____________

How many days is that? ____________

Page 20: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Flat or Stacked?

LabVIEW has two kinds of Sequence structures: Flat Sequence structures, in which the frames

are laid out side by side, as in the image on the previous slide.

Stacked Sequence structures, in which the frames are “stacked” like a deck of cards, with only one visible at a time.

(Bishop, p. 247)

Page 21: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Flat or Stacked? (Continued)

The two kinds of Sequence structure are similar. The main difference is how much space they take up on the block diagram. But there are other differences, such as: If data is wired out through a tunnel from a frame

in a Flat Sequence structure, the data passes out as soon as that frame finishes executing.

If data is wired out through a tunnel from a frame in a Stacked Sequence structure, the data does not pass out until the entire structure finishes executing.

Page 22: EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week

Use Sequence Structures Only When Necessary

Sequence structures serve a valid purpose, but they also tend to hide parts of the program and interfere with the natural flow of data in LabVIEW.

Avoid using them unless you have a situation where you need to guarantee the order of execution, and LabVIEW’s natural data flow does not provide such a guarantee.

(Bishop, p. 250)