Professional Documents
Culture Documents
Chapter 4c
Chapter 4c
Both series of statements do exactly the same thing, but the one
on the right is easier to read and understand with less
indentation.
Switch Statement
(Another way to do if…else if)
Although multiple statements can be placed after each case statement without
requiring curly braces, each case statement must end with the break command
OR the next command will also be executed until a break is hit.
Typical switch command
switch (nGradePoints/10) case 7:
{ Console.WriteLine ("gotta C");
case 0: break;
case 1: case 8:
case 2: Console.WriteLine("gotta B");
case 3: break;
case 4: case 9:
case 5: case 10:
Console.WriteLine ("You Failed"); Console.WriteLine("gotta A");
break; break;
case 6: default:
Console.WriteLine ("gotta D"); Console.WriteLine ("bad grade");
break; break;
}
The switch expression can be
an integer
a char
a String
a boolean
– but check for values of true or false in the case statements
– probably really doesn’t make sense to switch on this
an expression that resolves to one of these
File I/O
StreamWriter and StreamReader