-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcontinue-works-for-switch.ps1
More file actions
46 lines (36 loc) · 747 Bytes
/
continue-works-for-switch.ps1
File metadata and controls
46 lines (36 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<#
.Synopsis
"switch" with "continue" inside "foreach".
.Description
If the idea is to skip 2 and 3 and process other values then the code of
this script is incorrect because "continue" works for "switch", not for
"foreach".
The correct code should either use a label:
:for foreach($e in $data) {
switch($e) {
2 {continue for}
3 {continue for}
}
"Process $e"
}
or use "if" instead of "switch":
foreach($e in $data) {
if ($e -eq 2) {continue}
if ($e -eq 3) {continue}
"Process $e"
}
or just use "switch" as a loop:
switch($data) {
2 {continue}
3 {continue}
default {"Process $_"}
}
#>
$data = 1..4
foreach($e in $data) {
switch($e) {
2 {continue}
3 {continue}
}
"Process $e"
}