2014년 10월 4일 토요일

LabVIEW Tutorial - 1 : About Loop(For, While)



1. While and For Loops

LabVIEW has the merits to learn and to implement an application fast then the text-based language such as C, C++, C#. But it also requires a specific structure for an algorithm to implement the program. 


This article tell you about the loop structure(While, For) to implement it.

While and For Loop is a method to make and repeat an algorithm.
What does it have a difference between it?

1) While Loops

An while structure is one of the loop method. It can make a decision for the repeat prior to running the loop. If conditional statement is false, an while loop will make the repeat for the algorithm in the while loop. In C Language, a programmer use a For Loop more than a While Loop. But LabVIEW user tends to use the While Loop than a For Loop. 


Let's take look at the below picture to understand a flowchart for an While Loop.


This picture show a while loop in the text-based language.  You can write the code that repeat infinitely when A statement is true. If A statement is true, the algorithm process B statement. But if A is false, then an while loop is ends up.

Additional Tips!!
When you use an while loop in the LabVIEW, it make an additional thread in the program. You have to manage a thread which is made by a user manually in the text-based language. In the LabVIEW, the compiler  can handle it. User don't make a control for an additional thread. 

If you add 3 while loops in the block diagram, this program make and run 3 threads in the CPU.

We will see how to use a while loop in the LabVIEW. First, Let's take a method to insert it. You can find and add this statement in the popup menu of the block diagram when you click the right button of your mouse. You also find a for statement at same place.


[Path : Function - Programming - Structure - For or While Loop]



I am going to introduce an while loop. You should repeat the code in the sub-diagram until the specific condition is true. You can find more information in the LabVIEW's help. When added an while loop in the block diagram, you can see the Repeat Terminal and the Conditional Terminal in the block diagram. 
  • Iteration Terminal : It defines the number of an iteration. 0 is the first value.
  • Conditional Terminal : It defines when the loop stops. You have to enter a boolean value to it. 
  • There are two settings for the Conditional Terminal: Continue If True and Stop If True. When set to Continue If True, the while loop runs only if a Boolean Value which is provided to the Conditional Terminal is true.

About the configuration for the Conditional Terminal
  The left picture is Stop If True. When set to it, the while loop runs only if value is false. 

Otherwise,

  The left picture is Continue If True. When set to it, the while loop runs only if value is true.
To switch the Conditional Terminal, you have to click a right button on the Conditional Terminal


The most important things for an while loop is the time to decide the Conditional Terminal is true or not. An While Loop make this decision after running the algorithm once. An While Loop have to run algorithm at least once.

The below picture show and make code for the While Loop to repeat 5 times. 




2) For Loops

A For Loop is a statement to repeat N times. N is the terminal which refers the number of the iteration.  You also  add conditional terminal to For when you use above LabVIEW 2012. 


You can find a For Loop as same method to add an While Loop. 
A For Loop is like a below picture.
  • Counter Terminal(N) : It defines the number of the iteration to run.
  • Iteration Terminal: It defines the number of the iteration which is running. 
As I mentioned, User can add the Conditional Terminal in above LabVIEW 2012. In order to use it, you have to click right-button of you mouse on selected a For Loop.


It this is checked, A For Loop is changed to below picture. At that time, the iteration will ends up when the Conditional Terminal is true or iteration is executed N times.


The most important thing in a For Loop is that this loop have to be executed N times. When N is 0, this loop is not executed. That is big difference between an While Loop and a For Loop.

While Loop is executed at least once. But For Loop can not be executed. 

The below picture show the code that executed 5 times.



This article tell you the introduction and difference for an While and For Loop.

댓글 없음:

댓글 쓰기