When & Which
When & Which?
Comparison
| Feature | for Loop |
while Loop |
for-generate Statement |
|---|---|---|---|
| Execution | Sequential | Sequential | Concurrent |
| Usage Location | Inside a process |
Inside a process |
Outside a process |
| Iteration | Fixed number of times | Repeats while a condition is true |
Creates N physical copies |
| Purpose | Algorithmic tasks | Searching or Polling | Hardware Replication |
Quick Guide
-
Use a
forloop when...- You need to repeat an action a specific number of times.
- Iterating through all the bits of a vector, initializing every address in a memory.
-
Use a
whileloop when...- You need to repeat an action until a condition changes, and you don't know how long that will take.
- Searching for the first occurrence of a value, waiting for a status flag to be set in a testbench.
-
Use a
for-generatestatement when...- You need to create multiple, regular instances of hardware components or concurrent statements.
- Building an N-bit register from N flip-flops, creating a chain of adders, connecting several identical modules to a bus.
Remember, all loops have the same syntax rules, so you just need to remember the structure. Good luck! :+1:
No comments to display
No comments to display