# Variables and Assignment

Source: https://rungs.dev/docs/structured-text/variables-and-assignment

> Declare and assign values to variables in Structured Text on rungs.dev. Learn naming rules, := assignment semantics, and how variables map to PLC tags during execution.

## Variable Naming [#variable-naming]

In Structured Text (ST), variables are the building blocks of your logic. They represent inputs, outputs, and internal states within your PLC program.

### Naming Rules [#naming-rules]

- Must begin with a letter (A–Z or a–z)

- Can contain letters, digits, and underscores ( \_ )

- No spaces, symbols (except \_ ), or starting digits

- Case-insensitive in most environments (but using consistent case is recommended)

### Good Naming Conventions [#good-naming-conventions]

- Use descriptive names that reflect the purpose of the variable.

- Use camelCase or PascalCase consistently (or whatever style your team/project uses).

- Prefixes can be helpful (e.g., is, has, num, val, btn, etc.)

Examples:

```iecst

Start_Command    // BOOL (0 or 1) trigger
Counter_Value    // DINT counter
Temperature_Set  // REAL setpoint (e.g., 23.5)
Valve_Open       // BOOL output signal
```

Avoid names like:

```iecst

x1, val, temp2, a123  // Not meaningful
```

## Assignment Operator [#assignment-operator]

In Structured Text, assignment is done using :=, not =.

```iecst
Variable := Value;
```

- := assigns the value on the right to the variable on the left.

- A semicolon ( ; ) must follow the assignment.

Examples:

```iecst
Motor_On := 1;
Counter := Counter + 1;
```

Note: The = symbol is used only in comparisons, such as IF x = 10 THEN.

## Tag References [#tag-references]

variables (also called tags) are categorized into:

### 1. Input Tags [#1-input-tags]

- Represent physical or logical input signals from sensors, buttons, etc.

- Usually read-only inside logic.

```iecst
Start_Button, Temp_Sensor, Limit_Switch
```

### 2. Output Tags [#2-output-tags]

- Represent signals that go out to actuators, motors, alarms, etc.

- Controlled by your logic.

```iecst
Motor_Start, Buzzer, Valve_Open
```

### 3. Internal Tags [#3-internal-tags]

- Variables used for intermediate logic, memory, and calculations.

- Not tied to physical I/O.

```iecst
Counter_Value, Temp_Average, isRunning
```

Each tag type can be used in ST just like normal variables, but their source (hardware or logic) determines how they behave in the program.

## Examples [#examples]

```iecst
// Start the motor when the start button is pressed
Motor_Start := Start_Button;

// Increment the counter value
Counter_Value := Counter_Value + 1;

// Turn on the fan if the temperature exceeds the setpoint
IF Temp_Sensor > Temp_Setpoint THEN
    Fan_On := 1;
END_IF;

// Reset the counter when the stop button is pressed
IF Stop_Button THEN
    Counter_Value := 0;
END_IF;
```

> **IDEA**
>
> **Practice**
>
> Practice in the [Count Up exercise](https://rungs.dev/exercises/count-up) — increment a counter and raise a Done flag when it reaches its target.