CS 320 n Visual Programming Lab VIEW Control
CS 320 n –Visual Programming Lab. VIEW Control Structures
What We Will Do Today • Learn about control structures in Lab. VIEW • class working with control structures Visual Programming Lab. VIEW Control Structures 2
For Loop • Just like in Alice, execute some code a fixed number of times • In Lab. VIEW the loop is dragged around the components to be executed repeatedly Visual Programming Lab. VIEW Control Structures 3
For Loop Mechanics Count terminal. Wire a number to this. Could be constant or result of operation Iteration terminal. Which execution of the loop is this? Starts at 0, not 1! Visual Programming Lab. VIEW Control Structures 4
Count value • Loops can’t execute fractional times • 0, 1, 2, 3, 4 … • Try wiring a floating point number to the count terminal – Not an error – normal rounding Visual Programming Coercion Dot Lab. VIEW Control Structures 5
Expected Output? • how many times does the loop execute? • what is the first value of the iteration counter? the last? • tough to see – add time delay Visual Programming Lab. VIEW Control Structures 6
Slowed down loop Visual Programming Lab. VIEW Control Structures 7
While loop • continue executing until while some condition is true (or until some condition is false) • condition can be anything that has a boolean output – output of function or result of a switch or button Visual Programming Lab. VIEW Control Structures 8
While Loop – Stopping Conditions Stop if true Continue if true Right click to change Visual Programming Lab. VIEW Control Structures 9
While loop • Pick and display random numbers until a number greater than. 99 is selected • Tunnel – takes information out of a loop when finished Visual Programming Lab. VIEW Control Structures 10
Shift Registers • To get information from one iteration of a loop to the next shift register are used • Pick random numbers and add them together until the sum exceeds 1000 • How to keep a running total of them sum? Visual Programming Lab. VIEW Control Structures 11
Does Not Work • Why not? Visual Programming Lab. VIEW Control Structures 12
Fix with Shift Register • right click on loop boundary and choose “add shift register” Data from last iteration of loop. Give initial value. Visual Programming Data to next iteration of loop Lab. VIEW Control Structures 13
Completed Version Visual Programming Lab. VIEW Control Structures 14
Class work • Create a Lab. VIEW program that picks random numbers • Pick random numbers until a value greater than 0. 9999 is picked – while loop • Display how many picks it takes to get a result greater than 0. 9999 • Run the experiment 10, 000 times • What is the average number of picks required? – use a for loop • When that is finished add to the program to answer these questions: – What were the maximum and minimum number of picks required? Visual Programming Lab. VIEW Control Structures 15
- Slides: 15