Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Foreach loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== SystemVerilog === [[SystemVerilog]] supports iteration over any vector or array type of any dimensionality using the <code>foreach</code> keyword. A trivial example iterates over an array of integers: {| class="wikitable" ! code !! prints |- | <syntaxhighlight lang="systemverilog"> int array_1d[] = '{ 3, 2, 1, 0 }; foreach array_1d[index] $display("array_1d[%0d]: %0d", index, array_1d[index]); </syntaxhighlight> | array_1d[0]: 3 array_1d[1]: 2 array_1d[2]: 1 array_1d[3]: 0 |} A more complex example iterates over an associative array of arrays of integers: {| class="wikitable" ! code !! prints |- | <syntaxhighlight lang="systemverilog"> int array_2d[string][] = '{ "tens": '{ 10, 11 }, "twenties": '{ 20, 21 } }; foreach array_2d[key,index] $display("array_2d[%s,%0d]: %0d", key, index, array_2d[key,index]); </syntaxhighlight> | array_2d[tens,0]: 10 array_2d[tens,1]: 11 array_2d[twenties,0]: 20 array_2d[twenties,1]: 21 |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)