Bird
Raised Fist0
PLC Programmingscripting~10 mins

Assignment and arithmetic in PLC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Assignment and arithmetic
Start
↓
Assign initial value
↓
Perform arithmetic operation
↓
Store result in variable
↓
Use or output result
↓
End
This flow shows how a value is assigned to a variable, then arithmetic is done, and the result is stored back for use.
Execution Sample
PLC Programming
VAR
  a : INT := 5;
  b : INT := 3;
  c : INT;
END_VAR

c := a + b;
Assigns 5 to a, 3 to b, then adds a and b and stores the result in c.
Execution Table
StepActionVariable aVariable bVariable cNotes
1Initialize a with 55undefinedundefineda is set to 5
2Initialize b with 353undefinedb is set to 3
3Compute c = a + b538c stores sum of a and b
4End of execution538All variables assigned
💡 Execution stops after assigning sum to c.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aundefined5555
bundefinedundefined333
cundefinedundefinedundefined88
Key Moments - 2 Insights
Why is variable c undefined before the arithmetic operation?
Because c is declared but not assigned until step 3 where c := a + b happens, as shown in execution_table row 3.
Can we use variable c before it is assigned?
No, using c before assignment would cause an error or undefined behavior, as c only gets a value at step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of variable b after step 2?
A5
B3
C8
Dundefined
💡 Hint
Check the 'Variable b' column in execution_table row 2.
At which step does variable c get its value assigned?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Variable c' columns in execution_table.
If we change a to 7, what will be the value of c after step 3?
A10
B8
C5
Dundefined
💡 Hint
c = a + b, so update a in variable_tracker and sum.
Concept Snapshot
Assignment and arithmetic in PLC:
- Declare variables with VAR
- Assign values using :=
- Perform arithmetic like c := a + b
- Variables hold values for later use
- Order matters: assign before use
Full Transcript
This example shows how in PLC programming, variables are declared and assigned values. First, variable a is set to 5, then b to 3. Next, c is assigned the sum of a and b, which is 8. Variables must be assigned before use to avoid errors. The execution table traces each step and variable state. Changing a's value changes c accordingly.

Practice

(1/5)
1. In PLC programming, which symbol is used to assign a value to a variable?
speed := 100;
easy
A. The ':=' symbol
B. The '=' symbol
C. The '==' symbol
D. The '->' symbol

Solution

  1. Step 1: Understand assignment syntax in PLC

    PLC uses ':=' to assign values to variables, not '=' or '=='.
  2. Step 2: Identify correct assignment operator

    The ':=' operator updates the variable with the new value.
  3. Final Answer:

    The ':=' symbol -> Option A
  4. Quick Check:

    Assignment operator = ':=' [OK]
Hint: Remember ':=' means assign value in PLC [OK]
Common Mistakes:
  • Using '=' instead of ':='
  • Confusing comparison '==' with assignment
  • Using arrow '->' mistakenly
  • Forgetting semicolon after assignment
2. Which of the following is the correct syntax to add 5 to a variable count in PLC?
easy
A. count := count + 5;
B. count = count + 5;
C. count += 5;
D. count :=+ 5;

Solution

  1. Step 1: Check assignment operator usage

    PLC uses ':=' for assignment, so '=' and '+=' are incorrect here.
  2. Step 2: Verify arithmetic addition syntax

    'count := count + 5;' correctly adds 5 to count and assigns it back.
  3. Final Answer:

    count := count + 5; -> Option A
  4. Quick Check:

    Use ':=' for assignment and '+' for addition [OK]
Hint: Use ':=' for assignment, '+' for addition [OK]
Common Mistakes:
  • Using '=' instead of ':='
  • Trying '+=' which is not valid in PLC
  • Writing ':=+' which is invalid
  • Missing semicolon
3. What is the value of result after executing the following PLC code?
temp := 10;
result := temp * 3 + 2;
medium
A. 12
B. 36
C. 30
D. 32

Solution

  1. Step 1: Calculate multiplication first

    temp * 3 = 10 * 3 = 30
  2. Step 2: Add 2 to the multiplication result

    30 + 2 = 32
  3. Final Answer:

    32 -> Option D
  4. Quick Check:

    10 * 3 + 2 = 32 [OK]
Hint: Multiply before adding, follow order of operations [OK]
Common Mistakes:
  • Adding before multiplying
  • Using wrong variable value
  • Ignoring operator precedence
  • Confusing temp and result variables
4. Identify the error in this PLC code snippet:
speed := 50
speed := speed + 10;
medium
A. Cannot add numbers in PLC
B. Using '=' instead of ':='
C. Missing semicolon after first assignment
D. Variable 'speed' not declared

Solution

  1. Step 1: Check statement endings

    PLC statements must end with a semicolon ';'. The first line misses it.
  2. Step 2: Confirm other syntax is correct

    Second line uses correct ':=' and addition syntax.
  3. Final Answer:

    Missing semicolon after first assignment -> Option C
  4. Quick Check:

    Each statement ends with ';' [OK]
Hint: Always end PLC statements with semicolon ';' [OK]
Common Mistakes:
  • Forgetting semicolon
  • Confusing '=' with ':='
  • Assuming addition not allowed
  • Ignoring variable declaration rules
5. You want to calculate the average speed from three sensor readings s1, s2, and s3 in PLC. Which code correctly assigns the average to avg_speed?
s1 := 40;
s2 := 50;
s3 := 60;
avg_speed := (s1 + s2 + s3) / 3;
hard
A. avg_speed = (s1 + s2 + s3) / 3;
B. avg_speed := (s1 + s2 + s3) / 3;
C. avg_speed := s1 + s2 + s3 / 3;
D. avg_speed := (s1 + s2 + s3) * 3;

Solution

  1. Step 1: Check correct assignment operator

    Use ':=' to assign the calculated value to avg_speed.
  2. Step 2: Verify arithmetic expression correctness

    Sum all three readings and divide by 3 inside parentheses for average.
  3. Final Answer:

    avg_speed := (s1 + s2 + s3) / 3; -> Option B
  4. Quick Check:

    Use ':=' and parentheses for correct average [OK]
Hint: Use parentheses to sum before dividing [OK]
Common Mistakes:
  • Using '=' instead of ':='
  • Missing parentheses causing wrong division
  • Multiplying instead of dividing
  • Forgetting semicolon
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard