For loop explanation

QMS_MAN

Member
Join Date
Jun 2015
Location
uk
Posts
9
Hi All,

Please could somebody explain step by step the following statement.


FOR I:=1 TO 100 BY 2 DO
IF ARR = 70
THEN J:=I;
EXIT;
END_IF;
END_FOR


Kind regards..
 
The code is traversing through the first 100 elements of array 'ARR' and saving into variable 'J' the index of the first element that has a value of 70.

FOR I:=1 TO 100 BY 2 DO

Not too familiar with the 'BY 2' part. My guess is that it means only look at every second element of the array. I'd consult manuals for whatever hardware or software environment you grabbed this bit of code from.
 
Hi All,

Please could somebody explain step by step the following statement.


FOR I:=1 TO 100 BY 2 DO
IF ARR = 70
THEN J:=I;
EXIT;
END_IF;
END_FOR


Kind regards..


FOR I: =1 is setting variable I to 1.

TO 100: this loop will run until I >= 100 and then break.

BY 2 is the incremental value. 1+2=3. 3+2=5. etc...

IF ARR = 70: This is saying that if any of the elements in the array are equal to 70 then do the following:

THEN J:= I: Set variable J equal to the element number of the the array which is equal to 70. After that, exit the for loop completely.

Ex. ARR = [144, 42, 23, 41, 34, 70, 34, 99, etc...]

(dependent on the processor, the array element will start at 0 or 1. In this example I'll say it starts at 0 since i'm more used to that.)

the loop looks at element #1. It says ARR[1] = 42. the IF statement is false so I wont do anything.

I increments by 2.

ARR[3] = 42. IF statement is false. It gets skipped.

I increments by 2.

ARR[5] = 70. That makes the IF statement true.

Now we set J = 5.

Now we exit the FOR loop because thats all the info we need due to the EXIT command.
 
Thanks to everyone for the answers, has been very use full and cleared up my confusion.

Kind Regards
 

Similar Topics

Hi, I'm quite new to Rockwell PLC and currently trying to implement the following if Sw1 then for i:=1 to 20 by 1 do xEnable := 1...
Replies
4
Views
143
Hi all, I'm connecting several 4-20mA sensors together in parallel (only one shown below) The enclosure is ABS plastic with metal backplate DAQ...
Replies
5
Views
264
I have always controlled servos in Rockwell motion using position loop. I have an application where one process will push against a servo...
Replies
3
Views
266
Hello All Could we get some expertise on flow control ? -Using a PID loop in Productivity 2000 with an analog output, How can we convert...
Replies
19
Views
1,564
Complete noob here, using a Click C0-02DD1-D to run a test stand. Requirement is to turn on motor run for X seconds, turn off and dwell for X...
Replies
6
Views
1,067
Back
Top Bottom