If, Then, Else Statements

For Relay Ladder Logic (RLL) diagrams, your symbols must distinguish between relay contacts (inputs) "---| |---" and relay coils (results or outputs) "---( )---.
 
| A B |
|---| |-------( )---|
| |
| A C |
|---|/|-------( )---|
| |


 
Last edited:
The PLC ain't got an Else so instead, don't call them "if then else" statements, read them to yourself as "if then, if not then" as you write them in RLL just like Lancie did.
 
Last edited:
No!

The PLC ain't got an Else so instead, don't call them "if then else" statements, read them to yourself as "if then, if not then" as you write them in RLL just like lancie did.

Code:
If A THEN B // This state does not turn of B if false
IF NOT A THEN C // this statement does not turn of C if true
IF THEN statement jump over any code controlled by the if then statement when false. B is not set to false.
 
Okay, Peter, you are right, technically, strictly translating it the way you did is correct, my way and Lancie's method makes assumptions that without A then not B.
 
Adding to what Paul said, restructure your if-then-else statement to Boolean equations. Every if-then-else statement that assigns values should be able to be expressed as a Boolean equation and its inverse.

If A then B Else C becomes

B := A
C := NOT A

Now note the equivalence, since A = B then C = NOT B

This is useful because as you develop more complex logic, A can represent a compound Boolean, eg, lets say A is a compound of X AND (Y OR Z).
A = X AND (Y OR Z)

So in this case, make the assignment
B:= X AND (Y OR Z)

Now, instead of working out the inverse of that we use the previous equality and assign C as

C := NOT B

(lots simpler).
 
There is nothing in if A then B else C that would turn of B or C
You are over-thinking one little part of this: It is RELAY logic. I happen to know that for a Relay Coil B controlled by A Input, when the Input is OFF, the Relay Coil B goes off by default (it has no power). If this were not true, the hundreds of relay panels that I wired up in the past 60 years would not have worked!

So my logic is correct for RELAY LADDER LOGIC, which was the subject, not Boolean logic. Note that I started my first sentence with "For Relay Ladder Logic (RLL) diagrams, blah blah blah". RLL is a subset of the Boolean logic that you are talking about, and does not have exactly the same set of rules.
 
Last edited:
If A then B else C is not ladder language, if almost basic language or Structured Text like
The goal is to convert it into ladder language.
A text like language would convert to the assembly language like example I posted. Again, compile the ST version using SCL and look at the STL code that is generated.
 
I am not an STL guy, but I was going to go further to write that in ladder as
XIC A JMP LBL B
XIO A JMP LBL C

You can put what logic you want inside B or C (which could be anything from holding on an output, as Lancie and I assumed, or logic for measuring the change in an analog signal).

If a student question, then Lancie and I are probably giving them the answer they want to hear.

So Peter makes the point that to truly do "if then", the logic we like does not match. There is nothing said about reversing or undoing B in the Else portion. It is logic flow branching, so if you did OTE in one branch, and never revisit that branch when the If statement takes the Else branch, you would effectively be setting the output or latching it. It is branching, not bit assignments. Peter is correct, there is nothing in the If Then Else that says we must define the state of (assumed) output B, whereas that is exactly what Lancie and I and now Troy have done.
 
Last edited:
This has been covered before. It is perhaps easier to talk about what ladder looks like when translated into Structured Text instead of what ST looks like when translated into Ladder.

For instance, the simple rung:

A B
-----] [-----( )--



Would look like this when approached two different ways:
Code:
(* Computer Science 'Type' Translation *)
IF A THEN
    B := TRUE;
ELSE
    B := FALSE;
END_IF;

(* Safer Direct Assignment 'Type' Translation *)
B := A;
Inherent (maybe hidden) in the standard ladder output instruction is the fact that it is used to BOTH set and reset the output bit. That is why the IF with no ELSE clause has to be a set or reset type output -(S)- -(R)- because without the ELSE it can only do one or the other, but not both. This is why I call the B:=A; code 'Safer' than using IF/ELSE when replacing ladder with ST.
 

Similar Topics

Hi I'm starting a new job (so i'm a newbie in ftview programming ) and i have the following problem : I must update some pages in a supervision...
Replies
3
Views
5,197
11 months ago I ordered a 10" Redlion HMI. I was given an April delivery date. April came and went. I waited, and in June I asked our vendor...
Replies
7
Views
2,370
I'm about to have a guy onsite (I'm trying to help troubleshoot remotely) and he doesn't have the proper software. Usually I have the guys install...
Replies
4
Views
1,739
Hey so I have an issue that is driving me insane. I have 6 AB powerflex 527 drives configured in a DLR network over CIP Safety. They go back to a...
Replies
1
Views
1,724
Back
Top Bottom