EET 2259 Unit 5 Loops

29
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2. Lab #5 and Homework #5 due next week. Exam #1 next week.

description

EET 2259 Unit 5 Loops. Read Bishop, Sections 5.1 and 5.2. Lab #5 and Homework #5 due next week. Exam #1 next week. Structures. Structures control the flow of a program’s execution. This week we look at two kinds: For Loops While Loops Later we’ll look at other kinds: - PowerPoint PPT Presentation

Transcript of EET 2259 Unit 5 Loops

Page 1: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

EET 2259 Unit 5Loops

Read Bishop, Sections 5.1 and 5.2.

Lab #5 and Homework #5 due next week.

Exam #1 next week.

Page 2: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Breaking a Bad Habit

In prior weeks we’ve often used the Run Continuous button to run a program over and over. To stop such a program, we’ve used the Abort button.

But from now on, you should use loops instead of the Run Continuous button.

You should use the Abort button only as a last resort to stop a runaway program.

Page 3: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Structures

Structures (which in other programming languages are called control structures) control the flow of a program’s execution.

This week we look at two kinds: For Loops While Loops

Later we’ll look at other kinds: Sequence Structures Case Structures …

(Bishop, p. 213)

Page 4: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

For Loop

A For Loop executes the code inside its borders a specified number of times.

The code inside the For Loop’s borders is called a subdiagram.

A For Loop has two terminals: the count terminal and the iteration terminal.

(Bishop, p. 214)

Page 5: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

For Loop Example

Page 6: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Placing a For Loop

For Loops are found on the Functions>> Programming>> Structures palette.

Click it, and then drag to create a loop on the block diagram.

Then place items inside the loop to build your subdiagram.

(Bishop, p. 214)

Page 7: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Be Careful to Place Items Inside the Loop

If you’re not careful, you can end up with items “hovering” over a loop instead of being located inside the loop.

Correctly located inside the loop.

Not inside the loop, but hovering over it.

Page 8: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Count Terminal

A For Loop’s Count Terminal, labeled N, lets you set how many times the loop will execute.

You can set the count to a constant, or to a value set by the user through a control, or to the output of a function, etc.

The count is available to be used inside the loop.

(Bishop, p. 214)

Page 9: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Iteration Terminal

A For Loop’s Iteration Terminal, labeled i, contains the number of loop iterations that have been completed.

The iteration number is available to be used inside the loop.

It starts at 0 and increases to N-1.

(Bishop, p. 214)

Page 10: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Inserting a Time Delay Loops usually run so quickly that the user

can’t see what’s happening. To add a time delay, use either the old-

fashioned Wait (ms) function or the newer Time Delay Express VI.

Both are found on the Functions>> Programming >>Timing palette.

Place either one anywhere inside the loop.

Page 11: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Good Practice: Include a Time Delay in All Loops What if you don’t want to slow down the

loop to “human speed,” but want it to run as fast as possible?

It’s still a good programming practice to include a small (10 ms) delay in your loop. This prevents LabVIEW from consuming all of your processor’s time, and lets the operating system perform necessary tasks.

Page 12: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Tunnels

If a wire crosses the border of a loop (or other structure), a tunnel automatically appears on the border.

Doing this is often useful, but can also lead to confusion unless you keep in mind the following point….

Page 13: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Tunnels and Data Flow

No data passes into or out of a structure while the structure is executing.

Input data is read before the structure executes; subsequent changes to the input values are ignored.

Output data is not available until after the structure finishes executing.

Page 14: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Example For Loop in BASIC The type of loop we’ve been discussing is

called a “For Loop” because in text-based programming languages (such as BASIC or C++) it is coded using the word FOR.

Example:

CLS FOR i = 1 TO 15 PRINT i, 2 * i NEXT i

Page 15: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Integer Representations

Recall that blue terminals and blue wires represent integers.

Integer terminals can be further categorized into byte signed integer (I8), word signed integer (I16), long signed integer (I32), etc.

This is called the “representation” of the number, and you can change it by right-clicking on a terminal and choosing “Representation.”

Page 16: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Integer Representations (Continued) These representations differ in the range of

values they can handle and the amount of memory they use.Representation Max. Value Memory Bytes

Byte signed integer (I8) 127 1Word signed integer (I16) 32,767 2Long signed integer (I32) 2,147,483,647 4Quad signed integer (I64) 11019 8Byte unsigned integer (U8) 255 1

Word unsigned integer (U16) 65,535 2Long unsigned integer (U32) 4,294,967,295 4Quad unsigned integer (U64) 2 1019 8

Page 17: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Floating-Point Representations

Recall that orange terminals and orange wires represent floating–point numbers.

Floating-point terminals can be further categorized into single precision, double precision, and extended precision.

Page 18: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Floating-Point Representations (Continued)

These representations differ in the range of values they can handle and the amount of memory they use.

Representation Max. Value Memory BytesSingle-precision (SGL) 3.40 x 1038 4Double-precision (DBL) 1.79 x 10308 8

Extended-precision (EXT) 1.19 x 104932 16

Page 19: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Coercion Dots

If you wire together two terminals of different numeric representations, LabVIEW must convert the number from one representation to the other.

In these cases a red dot called a coercion dot will appear on the terminal where the conversion takes place.

Coercion dots are bad. They waste memory, and can lead to rounding errors that are difficult to find.

(Bishop, p. 216)

Page 20: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Numeric Conversion Functions

LabVIEW has functions for converting a number of any representation to any other representation. (For example, the To Word Integer function converts any number to the I16 representation.)

These functions are found on the Functions > Numeric > Conversion palette.

These are sometimes useful in eliminating coercion dots.

Page 21: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

While Loop

A While Loop executes the code inside its borders repeatedly until a certain condition is met.

A While Loop has two terminals: the iteration terminal and the conditional terminal.

(Bishop, p. 221)

Page 22: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

While Loop Example

Page 23: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Placing a While Loop

While Loops are found on the Functions >> Programming>> Structures palette.

Click it, and then drag to create a loop on the block diagram.

Then place items inside the loop to build your subdiagram.

Page 24: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Iteration Terminal

A While Loop’s Iteration Terminal, labeled i, contains the number of loop iterations that have been completed.

It behaves just like a For Loop’s iteration terminal.

The iteration number is available to be used inside the loop.

(Bishop, p. 221)

Page 25: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Conditional Terminal

A While Loop’s Conditional Terminal determines at the end of each loop execution whether the loop will be executed again.

You set the Conditional Terminal to either Stop if True or Continue if True.

Usually you’ll wire a Boolean control or the output of a Boolean function to this terminal.

(Bishop, p. 221)

Page 26: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Conditional Terminal:Stop if True

When the Conditional Terminal is set to Stop if True, it looks like a red stop sign on the block diagram.

A true condition will cause the loop to stop executing, but a false condition will cause it to execute again.

(Bishop, p. 221)

Page 27: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Conditional Terminal:Continue if True

When the Conditional Terminal is set to Continue if True, it looks like a green looping arrow on the block diagram.

A true condition will cause the loop to execute again, but a false condition will cause it to stop executing.

(Bishop, p. 221)

Page 28: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

Example While Loop in BASIC

This type of loop is called a “While Loop” because in text-based programming languages it is coded using the word WHILE.

Example:

CLS INPUT “Guess my age. ”, guess WHILE guess <> 46 INPUT “No. Try again. ”, guess WEND PRINT “You got it!”

Page 29: EET  2259 Unit  5 Loops

Floyd, Digital Fundamentals, 10th ed

For Loop With a Conditional Terminal

It’s possible to add a conditional terminal to a For Loop, creating a loop that behaves like a cross between a For Loop and a While Loop.

To do this, right-click a For Loop’s border and select Conditional Terminal.

(Bishop, p.220) We won’t use this feature in this course:

Whenever I refer to a For Loop, I mean a plain For Loop without a conditional terminal.