MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do you initialize an array in bulk with a FOR loop?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

How do you initialize an array in bulk with a FOR loop?

Array initialisation (zeroing, loading defaults, filling patterns) is a standard program duty: once at power-up, once at reset. A FOR loop is the general method; block instructions (FILL/MEMSet) are the efficient one — know both and choose by situation. The sections below cover the three patterns, the execution timing, and efficiency and taboos.

1. Three initialisation patterns

PatternCoding (ST)Use
Zero everythingFOR i:=1 TO 100 DO Data[i]:=0; END_FOR;Power-up/reset buffer clear
Load defaultsFOR i:=1 TO 20 DO Recipes[i].Temp:=Default[i]; END_FOR;Default recipe table
Fill a patternFOR i:=1 TO 50 DO Data[i]:=i; END_FOR;Index tables, test sequences

Block equivalents: pure zeroing is one FILL/FMOV/MEMSet (far faster than a loop); "load defaults" becomes a block copy when the defaults themselves are tabular (BMOV default table → array); pattern fills have no block substitute — FOR is the answer.

2. Execution timing: once means once

Initialisation belongs on the "first scan after power-up" (first-scan flag / first-scan bit) or inside the reset flow — running it every scan is a major error: the loop keeps刷ing user-edited data back to defaults, and the field symptom is "parameters bounce back after I change them". Checklist: the trigger for initialisation must be a one-shot event (power-up edge, reset-button edge), never a always-true condition.

TimingTriggerTypical content
Once at power-upFirst-scan flagArray clear, checksum check, default recipes
Once at resetReset button edgeClear runtime data, reload defaults
Once at mode switchSwitch edgeClear mode-related arrays (manual trace buffer)

3. The efficiency account: loop versus block

Loop cost ≈ iterations × instructions per iteration. A 1000-iteration clear in the main program may cost tens of microseconds to milliseconds — as a constant scan cost it matters; as a rare power-up event it does not. The principle: initialisation inside rare events (power-up/reset) may loop freely; anything on the constant scan path uses block instructions. Even block instructions on the constant path deserve scrutiny — re-flushing a large array every scan is a cost too.

One efficiency trick: for arrays needing repeated initialisation, switch to "incremental marking" — initialise only what was used (track a last_used index) instead of flushing everything.

4. Nesting and boundaries

Two-dimensional arrays (10 recipes × 20 parameters) take a double FOR: recipes outside, parameters inside, with distinct loop variables (i, j) — never reused. Three boundary rules: ① loop bounds match the array declaration exactly (ARRAY[1..20] means TO 20 — overshoot either errors out or silently corrupts a neighbour, platform-dependent); ② whether indexes start at 0 or 1 differs by platform — declaration and access must agree; ③ loop variables are integers, never floating point (precision plus efficiency, both lost).

5. Common errors

SymptomCauseFix
Parameters bounce back after editingInitialisation runs every scanTrigger on a one-shot event
Occasional scan-time spikesLarge-array loop on the constant pathMove to rare events or use block instructions
Tail of the array corruptedLoop upper bound overruns into a neighbourMatch bounds to the declaration
Initialisation seems not to runTrigger condition never trueMonitor the trigger flag and loop variables
Double loop scrambles resultsInner and outer share one variableSeparate i and j

6. Notes

The craft in array initialisation is not FOR syntax but "timing" and "boundaries": initialisation only on one-shot events (or user data gets flushed), bounds strictly matching the declaration (or neighbours get corrupted). Hold both and the FOR initialisation is a clean tool; lose either and it manufactures "mysteriously lost data" and "scan spikes".

7. References

FOR loops and arrays follow IEC 61131-3; block-fill instructions (FILL/FMOV/MEMSet) follow the platform's applied-instruction manual; first-scan flag behaviour follows each platform's system-device documentation; out-of-bounds behaviour follows the platform runtime (checked platforms error; unchecked ones corrupt neighbours at your own risk).

8. Cautions

⚠️ Three traps to avoid:
① Initialisation hanging on an always-true condition — user parameters flushed back every scan;
② Loop bound overrun silently writing into a neighbouring array;
③ A large-array loop on the constant scan path bloating the scan for nothing.

After writing initialisation, run two tests: edit a parameter, power-cycle immediately (retained or default per design), and monitor the loop variable's first and last values (they must equal the declared bounds).

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL