Вы находитесь на странице: 1из 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.

Review: Structures

Structures control the flow of a programs


execution.
Weve looked at two kinds:

For Loops
While Loops

This week well look at two more:

Case Structures
Sequence Structures

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)

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)

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)

Selector Terminal

A Case structure has one terminal, called


the selector terminal.
This terminal, labeled ?, determines which
one of the structures subdiagrams will be
executed.
Youll usually wire this terminal to a control
or to the output of a function.
(Bishop, p. 238)

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.

Selecting Which Case to Execute:


Numeric Example

A Case structure with a numeric selector


terminal can have any number of
subdiagrams: the one thats executed
depends on the value wired to the terminal.

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)

Adding and Deleting Cases

Right-click a Case structures 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)

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)

Tunnels

Recall that a tunnel automatically appears


on a structures 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, its an output tunnel.
If an output tunnel is wired on one of a Case
structures subdiagrams, then it must be
wired on all of the subdiagrams.
(Bishop, pp. 240-241)

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

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

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.

Sequence Structures

But in LabVIEW, you cant


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)

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)

Tick Count

The example on the previous slide used


LabVIEWs Tick Count function, which
returns the number of milliseconds (ms) on
your computers millisecond timer.
Generally this timer is reset to zero
whenever the computer is rebooted.
Knowing that Tick Count uses the U32
datatype, lets figure out how long it will
take for it to roll over to zero again.

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

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)

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.

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 LabVIEWs natural
data flow does not provide such a
guarantee.
(Bishop, p. 250)

Вам также может понравиться