Bird
Raised Fist0
PLC Programmingscripting~10 mins

Variable declaration in PLC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Variable declaration
Start
↓
Choose variable name
↓
Select data type
↓
Assign initial value?
No→Declare variable with default
|Yes↓
Assign initial value
↓
Variable declared
↓
End
This flow shows how a variable is declared by choosing a name, selecting a data type, optionally assigning an initial value, and completing the declaration.
Execution Sample
PLC Programming
VAR
  counter : INT := 0;
  motorOn : BOOL := FALSE;
END_VAR
Declares two variables: 'counter' as an integer starting at 0, and 'motorOn' as a boolean starting as FALSE.
Execution Table
StepActionVariableData TypeInitial ValueResult
1Declare variablecounterINT0counter created with value 0
2Declare variablemotorOnBOOLFALSEmotorOn created with value FALSE
3End declaration block---All variables declared
💡 All variables declared successfully, declaration block ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
counterundefined000
motorOnundefinedundefinedFALSEFALSE
Key Moments - 2 Insights
Why do we specify a data type when declaring a variable?
The data type tells the PLC what kind of value the variable will hold, like a number or true/false. This is shown in execution_table steps 1 and 2 where INT and BOOL are chosen.
What happens if we don't assign an initial value?
If no initial value is assigned, the variable may start with a default or unknown value. In this example, initial values are assigned, so the variables start with known values as seen in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the initial value of 'counter' after step 1?
AFALSE
Bundefined
C0
D1
💡 Hint
Check the 'Initial Value' column for step 1 in the execution_table.
At which step is the variable 'motorOn' declared?
AStep 1
BStep 2
CStep 3
DNot declared
💡 Hint
Look at the 'Variable' column in the execution_table.
If we remove the initial value for 'counter', what would likely change in the execution table?
AInitial Value for 'counter' would be undefined or default
BData Type would change
CVariable name would change
DDeclaration would fail
💡 Hint
Refer to the key moment about initial values and execution_table initial value column.
Concept Snapshot
Variable Declaration in PLC:
- Use VAR ... END_VAR block
- Syntax: name : TYPE := initial_value;
- Data type defines value kind (INT, BOOL, etc.)
- Initial value optional but recommended
- Declared variables hold values during program execution
Full Transcript
Variable declaration in PLC programming means creating a named storage space with a specific data type and optionally an initial value. The process starts by choosing a variable name, then selecting its data type like INT for integers or BOOL for true/false values. You can assign an initial value to start the variable with a known state. For example, 'counter' is declared as an INT with initial value 0, and 'motorOn' as BOOL with FALSE. This helps the PLC know what kind of data to expect and how to store it. If no initial value is given, the variable might start with a default or unknown value. Declaring variables properly is essential for clear and error-free PLC programs.

Practice

(1/5)
1. What is the purpose of declaring a variable in a PLC program?
easy
A. To display output on the screen
B. To create a new program file
C. To store data that the program can use and change
D. To connect to external devices

Solution

  1. Step 1: Understand variable role in PLC

    Variables hold data values that the program can read and modify during execution.
  2. Step 2: Compare options to variable purpose

    Only storing and changing data matches the role of variables.
  3. Final Answer:

    To store data that the program can use and change -> Option C
  4. Quick Check:

    Variables = store and change data [OK]
Hint: Variables hold data for program use and changes [OK]
Common Mistakes:
  • Thinking variables create files
  • Confusing variables with device connections
  • Assuming variables control display output
2. Which of the following is the correct way to declare an integer variable named Counter in a PLC program?
easy
A. VAR Counter = INT; END_VAR
B. VAR Counter INT END_VAR
C. VAR Counter : INTEGER END_VAR
D. VAR Counter : INT; END_VAR

Solution

  1. Step 1: Recall correct syntax for variable declaration

    Variables are declared with VAR, name, colon, type, and END_VAR to close.
  2. Step 2: Check each option's syntax

    VAR Counter : INT; END_VAR uses colon and type INT correctly; others miss colon, use wrong type name, or wrong assignment.
  3. Final Answer:

    VAR Counter : INT; END_VAR -> Option D
  4. Quick Check:

    Correct syntax = VAR name : type; END_VAR [OK]
Hint: Use colon between name and type in VAR block [OK]
Common Mistakes:
  • Omitting colon between variable name and type
  • Using '=' instead of ':' for type assignment
  • Using wrong type names like INTEGER instead of INT
3. Given the following PLC code snippet, what is the initial value of the variable Speed after declaration?
VAR
Speed : INT := 100;
END_VAR
medium
A. 100
B. 0
C. Undefined
D. Error

Solution

  1. Step 1: Identify variable initialization

    The variable Speed is declared as INT and initialized with := 100.
  2. Step 2: Understand initial value assignment

    Using := assigns the initial value 100 to Speed at declaration.
  3. Final Answer:

    100 -> Option A
  4. Quick Check:

    Initialization with := sets value to 100 [OK]
Hint: := sets initial value during declaration [OK]
Common Mistakes:
  • Assuming default zero without initialization
  • Thinking initialization causes error
  • Confusing := with assignment after declaration
4. Identify the error in the following variable declaration:
VAR
Temperature INT;
END_VAR
medium
A. Missing colon ':' between variable name and type
B. Missing END_VAR keyword
C. Variable name cannot start with uppercase letter
D. INT is not a valid data type

Solution

  1. Step 1: Review correct variable declaration syntax

    Variable declarations require a colon ':' between name and type.
  2. Step 2: Check given code for syntax errors

    The code misses ':' after Temperature before INT, causing syntax error.
  3. Final Answer:

    Missing colon ':' between variable name and type -> Option A
  4. Quick Check:

    Colon required between name and type [OK]
Hint: Always put ':' between variable name and type [OK]
Common Mistakes:
  • Omitting colon ':'
  • Assuming END_VAR is optional
  • Thinking variable names must be lowercase
5. You want to declare two variables in a PLC program: Count as an integer starting at 0, and Status as a boolean starting as TRUE. Which is the correct declaration?
hard
A. VAR Count INT := 0; Status BOOL := TRUE; END_VAR
B. VAR Count : INT := 0; Status : BOOL := TRUE; END_VAR
C. VAR Count : INT; Status : BOOL; END_VAR Count := 0; Status := TRUE;
D. VAR Count : INTEGER = 0; Status : BOOLEAN = TRUE; END_VAR

Solution

  1. Step 1: Check syntax for multiple variable declarations with initialization

    Each variable must have name, colon, type, and optional initialization with := inside VAR...END_VAR.
  2. Step 2: Evaluate each option's correctness

    VAR Count : INT := 0; Status : BOOL := TRUE; END_VAR correctly uses colon, types INT and BOOL, and initializes with := 0 and := TRUE. Others have syntax errors or wrong type names.
  3. Final Answer:

    VAR Count : INT := 0; Status : BOOL := TRUE; END_VAR -> Option B
  4. Quick Check:

    Use colon and := for initialization inside VAR block [OK]
Hint: Declare and initialize variables inside VAR...END_VAR with := [OK]
Common Mistakes:
  • Using '=' instead of ':=' for initialization
  • Omitting colon ':' between name and type
  • Using wrong type names like INTEGER or BOOLEAN
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