Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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.

2 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

3 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)

4 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)

5 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)

6 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)

7 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.

8 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.

9 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)

10 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)

11 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)

12 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)

13 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

14 Example CASE Statement in BASIC CLS INPUT “How many CDs are you buying? ”, CDs SELECT CASE CDs CASE IS >= 10 Cost = 8 * CDs CASE IS >= 5 Cost = 9 * CDs CASE ELSE Cost = 10 * CDs END SELECT PRINT “Total cost is $”; Cost

15 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.

16 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)

17 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)

18 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.

19 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? ____________

20 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)

21 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.

22 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)


Download ppt "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."

Similar presentations


Ads by Google