reduse code

hapetter

Member
Join Date
Feb 2009
Location
Kristiansand
Posts
58
Hi,

I want to make a dynamic if sentence. This is my code:

IF i = 4 OR i = 7 OR i = 10 OR i = 13 OR i = 16 OR i = 19 OR

So I like to make this more compact and such that I only specify the last number in if sentence. So there must be a way to use the repeating pattern to make this more compact and dynamic? Any tip?

Thanks
 
In which language are you writing the code?

If it's in C,C++ or Java, and if I remember the syntax correctly you can use a "for loop" like this(assuming last number is 19):

Code:
for(i=4; i <= 19; i + 3 )
  { // Write the code that you want to run conditionally here
  }
 
You dont write what PLC or programming language.

Many programming languages allow to make CASE statements with single values, a range of values or a series of values as selector.

like this:

CASE parameter OF
1 : ...... // single value
2,3,8 : ....... // series
10..20 : ...... // range
 
You could use the following function to perform the dynamic if

Code:
FUNCTION FC789:BOOL
//dynamic if statement
VAR_INPUT
iVar:INT;
iForstart:INT;
iForStep:INT;
iForEnd:INT;
END_VAR
VAR_TEMP
iIndex:INT;
END_VAR
BEGIN
FC789:=false;
FOR iIndex:=iForstart TO iForEnd BY iForStep DO
 IF iIndex=iVar THEN
  FC789:=True;
 END_IF;
end_for;
END_FUNCTION;
 

Similar Topics

Hi All, Can anyone advise me on how to determine the AN of a genie instance I writing to. I have created a genie to display a valve and some text...
Replies
0
Views
81
Hi I have been knocking my head against the wall trying to figure out why these two plcs won't talk with Produced and Consumed Tags data. The...
Replies
14
Views
417
Hello Everyone. Looking to see if any of you have encountered an issue with these drives. We have a major installation with around 30 of these...
Replies
0
Views
59
Error description: "A connection could not be established in the open process of the TCP connection." Action: •Check the operation of the...
Replies
3
Views
102
I am trying to display a variable within a cicode function onto a graphics page. This function queries a SQL database. I can get the value onto a...
Replies
3
Views
230
Back
Top Bottom