Skip to content

Instantly share code, notes, and snippets.

View Sambardo's full-sized avatar

Kory Sambardo

View GitHub Profile
@Sambardo
Sambardo / SwitchSample.cs
Created February 28, 2018 19:08
Sample for blog post
int i = 1;
switch (i)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
@Sambardo
Sambardo / SwitchSample1.ps1
Created February 28, 2018 19:10
Switch sample for blog
$i = 1;
switch ($i)
{
1 {
write-host "one"
break
}
2 {
write-host "two"
@Sambardo
Sambardo / BasicSwitchSample.ps1
Last active February 28, 2018 19:20
basic -eq sample for blog
switch("hello") #will match uppercase
{
"HELLO" {write-host "Uppercase" -ForegroundColor Magenta;break}
"hello" {write-host "lowercase" -ForegroundColor green;break}
}
switch("hello") #will NOT match h*
{
"h*" {write-host "wildcard" -ForegroundColor Magenta;break}
"hello" {write-host "lowercase" -ForegroundColor green;break}
@Sambardo
Sambardo / ExpressionSwitchSample.ps1
Created February 28, 2018 19:21
simple expression match for blog
$num = 4
Switch($num)
{
{$num -lt 5} {write-host "less than 5!" -ForegroundColor Magenta;break}
{$num -lt 10} {write-host "less than 10!" -ForegroundColor green;break}
{$num -lt 15} {write-host "less than 15!" -ForegroundColor cyan;break}
default {write-host "greater than or equal to 15" -ForegroundColor yellow}
}
$num = 4
Switch($num)
{
{$num -lt 5} {write-host "less than 5!" -ForegroundColor Magenta}
{$num -lt 10} {write-host "less than 10!" -ForegroundColor green}
{$num -lt 15} {write-host "less than 15!" -ForegroundColor cyan}
default {write-host "greater than or equal to 15" -ForegroundColor yellow}
}
$nums = 1..15
foreach($num in $nums)
{
Switch($num)
{
{$num -lt 5} {write-host "$num is less than 5!" -ForegroundColor Magenta}
{$num -lt 10} {write-host "$num is less than 10!" -ForegroundColor green}
{$num -lt 15} {write-host "$num is less than 15!" -ForegroundColor cyan}
default {write-host "$num is greater than or equal to 15" -ForegroundColor yellow}
@Sambardo
Sambardo / LoopSwitchSample2.ps1
Created February 28, 2018 19:24
use the built in loop of the switch, no foreach required
$nums = 1..15
Switch($nums)
{
{$_ -lt 5} {write-host "$_ is less than 5!" -ForegroundColor Magenta}
{$_ -lt 10} {write-host "$_ is less than 10!" -ForegroundColor green}
{$_ -lt 15} {write-host "$_ is less than 15!" -ForegroundColor cyan}
default {write-host "$_ is greater than or equal to 15" -ForegroundColor yellow}
}
@Sambardo
Sambardo / LoopSwitchSample3.ps1
Created February 28, 2018 19:24
careful with break!
$nums = 1..15
Switch($nums)
{
{$_ -lt 5} {write-host "$_ is less than 5!" -ForegroundColor Magenta;break}
{$_ -lt 10} {write-host "$_ is less than 10!" -ForegroundColor green;break}
{$_ -lt 15} {write-host "$_ is less than 15!" -ForegroundColor cyan;break}
default {write-host "$_ is greater than or equal to 15" -ForegroundColor yellow}
}
@Sambardo
Sambardo / LoopSwitchSample4.ps1
Created February 28, 2018 19:25
continue works instead of break with the loop feature
$nums = 1..15
Switch($nums)
{
{$_ -lt 5} {write-host "$_ is less than 5!" -ForegroundColor Magenta;continue}
{$_ -lt 10} {write-host "$_ is less than 10!" -ForegroundColor green;continue}
{$_ -lt 15} {write-host "$_ is less than 15!" -ForegroundColor cyan;continue}
default {write-host "$_ is greater than or equal to 15" -ForegroundColor yellow}
}
switch -casesensitive ("hello") #makes it a -ceq
{
"HELLO" {write-host "Uppercase" -ForegroundColor Magenta}
"hello" {write-host "lowercase" -ForegroundColor green}
"h*" {write-host "wildcard" -ForegroundColor Magenta}
}
switch -wildcard ("hello") #makes it a -like
{
"HELLO" {write-host "Uppercase" -ForegroundColor Magenta}