260 likes | 366 Views
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:
E N D
EET 2259 Unit 5Loops • 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: • Sequence Structures • Case Structures (Bishop, p. 213)
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)
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)
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)
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)
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. • Simply place either one anywhere inside the loop.
Tunnels • If a wire crosses the border of a loop (or other structure), a tunnel automatically appears on the border. • Doing this can be useful, but can also lead to confusion unless you keep in mind the following point….
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.
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
Integer Representations • Recall that blue terminals and blue wires represent integers. • Integer terminals can be further categorized into byte integer (I8), word integer (I16), long integer (I32), quad integer (I64), etc. • This is called the “representation” of the number, and you can change it by right-clicking on a terminal and choosing “Representation.”
Integer Representations (Continued) • These representations differ in the range of values that they can handle and the amount of memory that they use. • Byte integer (I8) max. = 127 • Word integer (I16) max. = 32,767 • Long integer (I32) max. = 2,147,483,647 • Quad integer (I64) max. 11019
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.
Floating-Point Representations (Continued) • These representations differ in the range of values that they can handle and the amount of memory that they use. • Single-precision max. 3.40 x 1038 • Double-precision max. 1.79 x 10308 • Extended-precision max. 1.19 x 104932
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)
Numeric Conversion Functions • LabVIEW has functions that let you convert a number of any representation to any other representation. (For example, using the To Word Integer function you can convert any number on the block diagram to the I16 representation.) • These functions are found on the Functions > Numeric > Conversion palette.
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)
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.
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)
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)
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)
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)
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!”
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.