1 / 26

EET 2259 Unit 5 Loops

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:

jola
Download Presentation

EET 2259 Unit 5 Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EET 2259 Unit 5Loops • Read Bishop, Sections 5.1 and 5.2. • Lab #5 and Homework #5 due next week. • Exam #1 next week.

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

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

  4. For Loop Example

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

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

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

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

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

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

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

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

  13. 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.  11019

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

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

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

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

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

  19. While Loop Example

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

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

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

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

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

  25. 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!”

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

More Related