Presentation is loading. Please wait.

Presentation is loading. Please wait.

EET 2259 Unit 7 Case Structures; Sequence Structures

Similar presentations


Presentation on theme: "EET 2259 Unit 7 Case Structures; Sequence Structures"— 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. -Handouts: Slide 23 (on tick count computations) -This week’s big idea is case structure. We’ll also look at sequence structures.

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 -Example: Notification.vi from two weeks ago contains a loop, a sequence structure, and case structures. -In programs up to now, all of the code on block diagram executed whenever the program ran; case structures let a program decide which code to execute, depending on conditions defined by the programmer.

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) -Case structures, or something like them, exist in all programming languages.

4 Case Structure Versus the Select Function
We’ve already used the Select function, which is one way in LabVIEW of choosing between alternatives. A Case structure serves a similar purpose, but it’s more powerful and more flexible than the Select function. In other words, you can use a Case structure to do anything that you can do with a Select function, but not vice versa.

5 Case Structure Versus the Select Function (Cont’d.)
In simple cases like the one on the previous slide, a Select function works fine. But here are two situations where Case structures are clearly better than the Select function. You need to choose among many alternatives rather than just among two or three. Your choice is between different kinds of actions, such as choosing whether to display a string or to make an LED blink.

6 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) Demo; and point out selector label and selector terminal. Selector label Selector terminal

7 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 cards in a deck, so that only one is visible at a time. The selector label on the top border tells which case you’re looking at, and has arrow buttons to let you step through the cases.

8 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) -Bishop’s example on p. 201: place numeric controls labeled “First number” and “Second number,” a toggle switch labeled “Add or Subtract?,” a numeric indicator labeled “Result.” On block diagram, on False subdiagram wire add function, and on True subdiagram wire subtract function. -Run a few times; turn on execution highlighting to show how it selects one subdiagram or the other. -Show how you could same thing using a Select function. -Then place entire block diagram inside a While loop with a Stop button.

9 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. This is the example we’ve been dealing with.

10 Selecting Which Case to Execute: Numeric Example
A Case structure with a numeric (integer) selector terminal can have many subdiagrams: the one that’s executed depends on the value wired to the terminal. -First use a 2-position knob (rep=I8) with text labels to do same thing as above. Show that even though it has these text labels, its output is an integer. Then connect it to a case structure to perform the correct function. -Extending previous example, add two more text labels for Multiply, Divide. -You could also do this with Select functions, but it would be a lot harder.

11 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 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) Demo by adding a 5th case, “Product,” and have it do same case as “Multiply.”

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

13 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) Demo what happens if you don’t have a default case in the previous example.

14 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 ) -Show how tunnel is white and the run arrow is broken until output tunnels are wired on all cases.

15 Example if Statement in C++
cout << "How many CDs are you buying? "; cin >> CDs; if (CDs >= 5) { totalCost = 9 * CDs; cout << "You get a discount! "; } else totalCost = 10 * CDs; cout << "Total cost is $" << totalCost; -IF statement is good if you have two different possibilities.

16 Order of Execution in Text-Based Languages
In text-based programming languages, the order of the statements determines the order of execution.

17 Order of Execution in LabVIEW
In LabVIEW, if two functions are connected by wires, you can tell which one must execute first. But you can’t always predict the order of execution. For example, in this code, which function executes first: the Subtract or the Greater Than?

18 Example Program On this block diagram, in which order will these operations execute? a+b c+d Time delay -Demo with SeqStrucExample.vi.

19 Sequence Structures Sequence structures can be used to force code to execute in a specific sequence. 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) -Modify previous example. Run it, then run with execution highlighting on. Sequence structure with three frames

20 Measuring the Speed of Your Code
Here’s a basic application of a sequence structure. It shows how you can measure the time needed to execute some code by: Reading the computer’s internal clock. Executing the code whose execution time you want to measure. Reading the clock again and finding the difference between the two clock readings. Use template above to measure speed of plotting 1,000,000 random numbers. (Then cube each number before plotting using built-in function and using subVI from Week 4.)

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

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

23 Flat or Stacked? LabVIEW has two kinds of Sequence structures:
Flat Sequence structures, in which the frames are laid out side by side. Stacked Sequence structures, in which the frames are “stacked” like a deck of cards, with only one visible at a time. (Bishop, p. 247) -Use shortcut menu to convert previous example to a stacked sequence structure, and run again (with execution highlighting on).

24 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. -Continuing example from previous slide, convert it back to a flat structure, then move controls and indicators outside the frames so that data passes out through tunnels. Run it. -Then convert back to a stacked structure again, and run again.

25 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. Some programmers over-use them. 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) -Example: Look at sequence structure in Notification.vi.


Download ppt "EET 2259 Unit 7 Case Structures; Sequence Structures"

Similar presentations


Ads by Google