<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Doubt about the 'for loop' in Structure text!</title><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/f5f847cc71/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/f5f847cc71/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/CODESYS-V2/thread/f5f847cc71/</id><updated>2020-02-27T19:51:56.523000Z</updated><subtitle>Recent posts to Doubt about the 'for loop' in Structure text!</subtitle><entry><title>Doubt about the 'for loop' in Structure text!</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/f5f847cc71/?limit=25#d60a" rel="alternate"/><published>2020-02-27T19:51:56.523000Z</published><updated>2020-02-27T19:51:56.523000Z</updated><author><name>hcchin</name><uri>https://forge.codesys.com/u/hcchin/</uri></author><id>https://forge.codesys.come6963e7a2fb4e0d267d5eb403cb95af2726d306d</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thanks for your explanation..&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Doubt about the 'for loop' in Structure text!</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/f5f847cc71/?limit=25#4f40" rel="alternate"/><published>2020-02-27T19:51:52.845000Z</published><updated>2020-02-27T19:51:52.845000Z</updated><author><name>ndzied1</name><uri>https://forge.codesys.com/u/ndzied1/</uri></author><id>https://forge.codesys.coma14d959f6b7beba8794ea3c6b407562a0d6c050e</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;It is running 5 times for each scan of the program. &lt;/p&gt;
&lt;p&gt;Since the program gets continually scanned,  it will run another 5 times on the 2nd scan, 3rd scan, etc. &lt;/p&gt;
&lt;p&gt;Think of the main program in a PLC as being contained inside a "DO Forever" loop. &lt;/p&gt;
&lt;p&gt;If you want the FOR loop to execute only once, you need to add some sort of condition, perhaps an IF statement around it  &lt;/p&gt;
&lt;p&gt;Declair a flag&lt;/p&gt;
&lt;p&gt;xRunFor: BOOL := TRUE;&lt;/p&gt;
&lt;p&gt;Then in the code...&lt;/p&gt;
&lt;p&gt;IF xRunFor THEN&lt;/p&gt;
&lt;p&gt;... FOR Loop&lt;/p&gt;
&lt;p&gt;xRunFor := FALSE;&lt;/p&gt;
&lt;p&gt;END_IF;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Doubt about the 'for loop' in Structure text!</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/f5f847cc71/?limit=25#ee91" rel="alternate"/><published>2020-02-27T19:51:51.501000Z</published><updated>2020-02-27T19:51:51.501000Z</updated><author><name>hcchin</name><uri>https://forge.codesys.com/u/hcchin/</uri></author><id>https://forge.codesys.com313d882d72e5adebdce291a3a4f3e3c56a867cc1</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I am new to structure text and I don't understand how it work. please advise on the code below.&lt;/p&gt;
&lt;p&gt;PROGRAM PLC_PRG&lt;/p&gt;
&lt;p&gt;VAR&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;M1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;INT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;M2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;INT&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;END_VAR&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;FOR m1:=1 TO 5 BY 1 DO&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;m2&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="nt"&gt;m2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nt"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;END_FOR;&lt;/p&gt;
&lt;p&gt;this program should run 5 times and m2 should return value 6. &lt;/p&gt;
&lt;p&gt;but it didn't return value 6, and it is still running and adding on&lt;/p&gt;
&lt;p&gt;value to m2.  how can it be?....&lt;/p&gt;
&lt;p&gt;&lt;img alt="IMG: For.JPG" rel="nofollow" src="https://forum.codesys.com//download/file.php?id=165"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;a class="" href="https://forum.codesys.com//download/file.php?id=166" rel="nofollow"&gt;for.pro &lt;span&gt;&lt;span&gt;[23.29 KiB]&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>