"Loops and Structures": Electronics and Communication Department CCE402: Modeling and Simulation
"Loops and Structures": Electronics and Communication Department CCE402: Modeling and Simulation
"Loops and Structures": Electronics and Communication Department CCE402: Modeling and Simulation
Lecture 2:
By:
Dr. Aya Hossam
Today’s Menu
Topics of lecture (2):
Loops and Structures:
• While Loop
• For Loop
• Case structure
1. Loops and Structures
Repeat (code);
Until Condition met;
End;
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.
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;
• 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)
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