buhnen
Member
Hi all,
I need some help here please. I'm trying to understand the SCL programming example included in the SIEMENS SCL official documentation. My doubt is about the "bubble sort" algorythm (comparing one to one elements within an array for sorting them in ascendent or descendent order).
I copy and paste the code and translate the variables(in spanish) to english:
BEGIN
REPEAT
change := FALSE;
FOR index := LIMIT TO 1 BY -1 DO
IF sorting_buffer[index-1] > sorting_buffer[index] THEN
help := sorting_buffer[index];
sorting_buffer[index] := sorting_buffer[index-1];
sorting_buffer[index-1] := help;
change := TRUE;
END_IF;
END_FOR;
UNTIL NOT change
END_REPEAT;
LIMIT takes the value of 8 (as my "sorting_buffer" array has 8 elements from 0 to 7).
The counter (index) decreases 1 each time checking 8 times the comparison between an element and the previous one. If the IF condition is satisfied both elements will be re-sorted and "change" will set to TRUE. IF NOT, again untill the rest of those 8 times, the checking of the IF will be repeated.
Let's say that the last time the FOR instruction executes it is made a comparison and the IF executes as well. Then, "change" will be set to TRUE and the IF and the FOR instructions reach to the end. We go to the next line of the program and see "UNTIL NOT change", so "Change" must be FALSE so that to close the REPEAT loop...but we have said that "change" was SET to true before.
I don't understand the reason to be for "change" and the REPEAT instruction. Could you please help me to understand this?
Thanks in advance.
P.S- I quicly "learnt" a year ago the basics of C language. I say it just in case my doubt can sound of a very newbie thing.
I need some help here please. I'm trying to understand the SCL programming example included in the SIEMENS SCL official documentation. My doubt is about the "bubble sort" algorythm (comparing one to one elements within an array for sorting them in ascendent or descendent order).
I copy and paste the code and translate the variables(in spanish) to english:
BEGIN
REPEAT
change := FALSE;
FOR index := LIMIT TO 1 BY -1 DO
IF sorting_buffer[index-1] > sorting_buffer[index] THEN
help := sorting_buffer[index];
sorting_buffer[index] := sorting_buffer[index-1];
sorting_buffer[index-1] := help;
change := TRUE;
END_IF;
END_FOR;
UNTIL NOT change
END_REPEAT;
LIMIT takes the value of 8 (as my "sorting_buffer" array has 8 elements from 0 to 7).
The counter (index) decreases 1 each time checking 8 times the comparison between an element and the previous one. If the IF condition is satisfied both elements will be re-sorted and "change" will set to TRUE. IF NOT, again untill the rest of those 8 times, the checking of the IF will be repeated.
Let's say that the last time the FOR instruction executes it is made a comparison and the IF executes as well. Then, "change" will be set to TRUE and the IF and the FOR instructions reach to the end. We go to the next line of the program and see "UNTIL NOT change", so "Change" must be FALSE so that to close the REPEAT loop...but we have said that "change" was SET to true before.
I don't understand the reason to be for "change" and the REPEAT instruction. Could you please help me to understand this?
Thanks in advance.
P.S- I quicly "learnt" a year ago the basics of C language. I say it just in case my doubt can sound of a very newbie thing.