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 guys, I have the first Kinetix 5100 in my facilities. I already configure the movement in Speed Mode, I am sending the Analog signal from my...
Replies
1
Views
59
Hi, I wrote a sequence loop using EQU and MOV instructions and would like to validate that i'm doing this properly. i attached a picture of the...
Replies
11
Views
371
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
9
Views
415
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
315
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
302
Back
Top Bottom