You are viewing an older version of the site. Click here to view
the latest version of this page. (This may be a dead link, if so, try the root page of the docs
here.)
== foreachelse ==
[[../objects/ms.lang.RangeException|ms.lang.RangeException]] |- ! scope="row" | Since | 3.3.1 |- ! scope="row" | Restricted |
Copy Code
The output would be:
Copy Code
The output would be:
Works like a foreach, except if the array is empty, the else code runs instead. That is, if the code would not run at all, the else condition would. In general, brace syntax and use of foreach(){ } else { } syntax is preferred, instead of using foreachelse directly.
=== Vital Info ===
{| style="width: 40%;" cellspacing="1" cellpadding="1" border="1" class="wikitable"
|-
! scope="col" width="20%" |
! scope="col" width="80%" |
|-
! scope="row" | Name
| foreachelse
|-
! scope="row" | Returns
| void
|-
! scope="row" | Usages
| array, [key], ivar, code, else
|-
! scope="row" | Throws
| [[../objects/ms.lang.CastException|ms.lang.CastException]][[../objects/ms.lang.RangeException|ms.lang.RangeException]] |- ! scope="row" | Since | 3.3.1 |- ! scope="row" | Restricted |
No
|-
! scope="row" | Optimizations
| None
|}
=== Usages ===
foreachelse(array, [key], ivar, code, else)=== Examples === ====Example 1==== Basic usage, with the else code not running Given the following code:
@array = array(1, 2, 3)
foreachelse(@array, @val,
msg(@val)
, #else
msg('No values in the array')
)

1 @array = {{function|array}}(1, 2, 3)
2 {{function|foreachelse}}(@array, @val,
3 {{function|msg}}(@val)
4 , #else
5
6 {{function|msg}}('No values in the array')
7 )
1 2 3====Example 2==== Empty array, so else block running Given the following code:
@array = array()
foreachelse(@array, @val,
msg(@val)
, #else
msg('No values in the array')
)

1 @array = {{function|array}}()
2 {{function|foreachelse}}(@array, @val,
3 {{function|msg}}(@val)
4 , #else
5
6 {{function|msg}}('No values in the array')
7 )
No values in the array===See Also===
[[API/functions/foreach.html|foreach]]
, [[Loops|Learning Trail: Loops]], [[Array_Iteration|Learning Trail: Array Iteration]]Find a bug in this page? Edit this page yourself, then submit a pull request. (Note this page is automatically generated from the documentation in the source code.)