"Loops and Structures": Electronics and Communication Department CCE402: Modeling and Simulation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Electronics and Communication Department

CCE402: Modeling and Simulation

Lecture 2:

“Loops and Structures”

By:
Dr. Aya Hossam
Today’s Menu
 Topics of lecture (2):
Loops and Structures:
• While Loop
• For Loop
• Case structure
1. Loops and Structures

• In addition to controls, indicators, and VIs, the block While loop


diagram can also contain a number of programming
structures that modify the sequence of data flow on the
block diagram.

• These structures perform traditional functions like For loop


looping or case-selection.

• LabVIEW currently supports six different structures --


while-loops, case structures, event structures, for-loops, Case
sequence structures, and formula nodes. Structure
1. Loops and Structures
1. Select the loop using: structures >> 2. Enclose code to be repeated
Loop_Name.

3. Drop or drag additional nodes and then wire


While Loop

Repeat (code);
Until Condition met;
End;

LabVIEW While Loop Flowchart Pseudo Code


While Loop
• Iteration terminal: returns number of times loop has executed; zero indexed
• Conditional terminal: defines when the loop stops. Default: “Stop if True”.

Iteration Terminal
Conditional Terminal
While Loop
Structure Tunnels
• Tunnels feed data into and out of structures.

• The tunnel is a block that appears on the border; the color of the block is related to
the data type wired to the tunnel.

• When a tunnel passes data into a loop, the loop executes only after data arrive at
the tunnel.

• Data pass out of a loop after the loop terminates.


Examples on “While Loop”

1. Add two numbers within While loop and make this example run until the
user clicks the stop button
Examples on “While Loop”
2. Create a simple while loop that generates random numbers and displays
them until the Stop button is pressed
Examples on “While Loop”
3. Repeat last example but now display the final random number from while
loop using Tunnel.
“The result in Numeric indicator is the result after ending the while loop.”
For Loop

N=100;
i=0;
Until i=N:
Repeat (code ; i=i+1);
End;

LabVIEW While Loop Flowchart Pseudo Code


For Loop
• Create a For Loop the same way you create a While Loop.

• If you need to replace an existing While Loop with a For Loop, right-click the border
of the While Loop, and select Replace with For Loop from the shortcut menu.

• The value in the count terminal (an input terminal) indicates how many times to
repeat the sub-diagram.

Count terminal
(Numerical input)

Wait Until Next ms


Multiple function
For Loop
For Loops – Conditional Terminal
You can add a conditional terminal to configure a For Loop to stop when
a Boolean condition or an error occurs
For Loop
For Loops – Conditional Terminal
For Loops configured for a conditional exit have:
• Red glyph next to the count terminal.
• Conditional terminal in the lower right corner.
Examples on “For Loop”
1. Create a simple For loop that multiply random numbers by constant value
= 30 and displays them through tunnel.
Examples on “For Loop”
2. Repeat the last example but now use the shift register and add the value
registered in it with constant 5.
Case Structures
• Have two or more sub-diagrams or cases
• Execute and displays only one case at a time
• An input value determines which sub-diagram to execute
• Similar to case statements or if...then...else statements in text-based
programming languages
Case Structures
• Case Selector Label: contains the name of the current case and
decrement and increment buttons on each side

• Selector Terminal: Wire an input value, or selector, to determine


which case executes
Case Structures
 Default Case
• You can specify a default case for the
Case structure.
− If you specified cases for 1, 2, and 3, but
you get an input of 4, the Case structure
executes the default case.

• Right-click the Case structure border to


add, duplicate, remove, or rearrange
cases and to select a default case.
Case Structures
 Input & Output Tunnels
You can create multiple input and output tunnels
• Inputs are available to all cases if needed
• You must define each output tunnel for each case
Case Structures
 Use Default if Unwired
Default values are: Data Type Default Value

Numeric 0

Boolean FALSE

String Empty

Avoid using the Use Default If Unwired option on Case structure tunnels
• Adds a level of complexity to your code
• Complicates debugging your code
Examples on “Case Structure”
1. Create a simple Case structure VI that turn on two leds if case = 1 and
turn off if case=0.
Examples on “Case Structure”
1. Create a simple Case structure VI that Calculate the square root of any
numbers that greater than 0.
Timing a VI
Why do you need timing in a VI?
• Control the frequency at which a loop executes.
• Provide the processor with time to complete other tasks, such as processing
the user interface.
Wait Functions:
• A wait function inside a loop allows the VI to sleep for a set amount of time
• Allows the processor to address other tasks during the wait time
• Uses the operating system millisecond clock

You might also like