有什么方法可以代替if语句来创建更好的代码?

问题描述 投票:0回答:1

我正在寻找避免if语句并创建更好的代码的方法,因为我必须在多个PLC中编写它,我认为这是浪费时间,但是我无法弄清楚如何制作代码块,可以做到相同但长度的一小部分。我对PLC编程和一般编码非常陌生。

这里是主要的if语句:

hibak:=0;
If HMI_C1_Defect_Alarm1=1 then 
hibak:=10;
elsif HMI_C1_Defect_Alarm2=1 then
hibak:=11;
elsif HMI_C1_Defect_Alarm3=1 then 
hibak:=12;
elsif HMI_C1_Defect_Alarm4=1 then
hibak:=13;
elsif HMI_C1_Defect_Alarm6=1 then
hibak:=14;
elsif HMI_C1_Defect_Alarm7=1 then 
hibak:=15;
elsif HMI_C1_Defect_Alarm8=1 then
hibak:=16;
elsif HMI_C1_Defect_Alarm9=1 then 
hibak:=17;
elsif HMI_C1_Defect_Alarm10=1 then
hibak:=18;
elsif HMI_C1_Defect_Alarm11=1 then 
hibak:=19;
elsif HMI_C1_Defect_Alarm12=1 then
hibak:=20;
elsif HMI_C1_Defect_Alarm13=1 then 
hibak:=21;
elsif HMI_C1_Defect_Alarm14=1 then
hibak:=22;
elsif HMI_ST1_Defect_Alarm1_1=1 then 
hibak:=23;
elsif HMI_ST1_Defect_Alarm1_2=1 then
hibak:=24;
elsif HMI_ST1_Defect_Alarm1_3=1 then 
hibak:=25;
elsif HMI_ST1_Defect_Alarm1_4=1 then
hibak:=26;
elsif HMI_ST1_Defect_Alarm1_5=1 then 
hibak:=27;
elsif HMI_ST1_Defect_Alarm1_6=1 then
hibak:=28;
elsif HMI_ST1_Defect_Minor1_1=1 then
hibak:=29;
elsif HMI_ST1_Defect_Minor1_2=1 then
hibak:=30;
elsif HMI_ST1_Defect_Minor1_3=1 then
hibak:=31;
elsif HMI_ST1_Defect_Minor1_4=1 then
hibak:=32;
elsif HMI_ST1_Defect_Minor1_5=1 then
hibak:=33;
elsif HMI_ST1_Defect_Minor1_6=1 then
hibak:=34;
elsif HMI_ST1_Defect_Minor1_7=1 then
hibak:=35;
elsif HMI_ST1_Defect_Minor1_8=1 then
hibak:=36;
elsif HMI_ST1_Defect_Minor1_9=1 then
hibak:=37;
elsif HMI_ST1_Defect_Minor1_10=1 then
hibak:=38;
elsif HMI_ST1_Defect_Minor1_11=1 then
hibak:=39;
elsif HMI_ST1_Defect_Minor1_12=1 then
hibak:=40;
elsif HMI_ST2_Defect_Minor1_1=1 then
hibak:=41;
elsif HMI_ST2_Defect_Minor1_2=1 then
hibak:=42;
elsif HMI_ST2_Defect_Minor1_3=1 then
hibak:=43;
elsif HMI_ST2_Defect_Minor1_4=1 then
hibak:=44;
elsif HMI_ST2_Defect_Minor1_5=1 then
hibak:=45;
elsif HMI_ST2_Defect_Minor1_6=1 then
hibak:=46;
elsif HMI_ST3_Defect_Alarm1_1=1 then
hibak:=47;
elsif HMI_ST3_Defect_Alarm1_2=1 then
hibak:=48;
elsif HMI_ST3_Defect_Alarm1_3=1 then
hibak:=49;
elsif HMI_ST3_Defect_Alarm1_4=1 then
hibak:=50;
elsif HMI_ST3_Defect_Minor1_1=1 then
hibak:=51;
elsif HMI_ST3_Defect_Minor1_2=1 then
hibak:=52;
elsif HMI_ST3_Defect_Minor1_3=1 then
hibak:=53;
elsif HMI_ST3_Defect_Minor1_4=1 then
hibak:=54;
elsif HMI_ST3_Defect_Minor1_5=1 then
hibak:=55;
elsif HMI_ST3_Defect_Minor1_6=1 then
hibak:=56;
elsif HMI_ST3_Defect_Minor1_7=1 then
hibak:=57;
elsif HMI_ST3_Defect_Minor1_8=1 then
hibak:=58;
elsif HMI_ST3_Defect_Minor1_9=1 then
hibak:=59;
elsif HMI_ST3_Defect_Minor1_10=1 then
hibak:=60;
elsif HMI_ST3_Defect_Minor1_11=1 then
hibak:=61;
elsif HMI_ST3_Defect_Minor1_12=1 then
hibak:=62;
elsif HMI_ST3_Defect_Minor1_13=1 then
hibak:=63;
elsif HMI_ST3_Defect_Minor1_14=1 then
hibak:=64;
elsif HMI_ST3_Defect_Minor1_15=1 then
hibak:=65;
elsif HMI_ST3_Defect_Minor1_16=1 then
hibak:=66;
elsif HMI_ST3_Defect_Minor2_1=1 then
hibak:=67;
elsif HMI_ST3_Defect_Minor2_2=1 then
hibak:=68;
elsif HMI_ST4_Defect_Alarm1_1=1 then
hibak:=69;
elsif HMI_ST4_Defect_Alarm1_2=1 then
hibak:=70;
elsif HMI_ST4_Defect_Alarm1_3=1 then
hibak:=71;
elsif HMI_ST4_Defect_Alarm1_4=1 then
hibak:=72;
elsif HMI_ST4_Defect_Minor1_1=1 then
hibak:=73;
elsif HMI_ST4_Defect_Minor1_2=1 then
hibak:=74;
elsif HMI_ST4_Defect_Minor1_3=1 then
hibak:=75;
elsif HMI_ST4_Defect_Minor1_4=1 then
hibak:=76;
elsif HMI_ST4_Defect_Minor1_5=1 then
hibak:=77;
elsif HMI_ST4_Defect_Minor1_6=1 then
hibak:=78;
elsif HMI_ST4_Defect_Minor1_7=1 then
hibak:=79;
elsif HMI_ST4_Defect_Minor1_8=1 then
hibak:=80;
elsif HMI_ST4_Defect_Minor1_9=1 then
hibak:=81;
elsif HMI_ST4_Defect_Minor1_10=1 then
hibak:=82;
elsif HMI_ST4_Defect_Minor1_11=1 then
hibak:=83;
elsif HMI_ST4_Defect_Minor1_12=1 then
hibak:=84;
elsif HMI_ST4_Defect_Minor1_13=1 then
hibak:=85;
elsif HMI_ST4_Defect_Minor1_14=1 then
hibak:=86;
elsif HMI_ST4_Defect_Minor1_15=1 then
hibak:=87;
elsif HMI_ST4_Defect_Minor1_16=1 then
hibak:=88;

end_if; 

我会尽力而为。预先非常感谢!

if-statement plc
1个回答
0
投票

不幸的是,这是一段脱离上下文的代码。有很多方法可以提高代码质量,一种方法是调整软件体系结构,从而避免这种未经改进的编码风格。

© www.soinside.com 2019 - 2024. All rights reserved.