Session 11-12 switch statement in C #
Switch statement
Break statement If break statement is used inside a switch statement,he control would leave the switch statemen T
Goto statement You can either jump to another case statement,or to specific label (label are some word you codin G to mark).
Warning:using Goto is bad programming style,we can should avoid go to by all means
Multiple if Else statements can be replaced with a switch statement
For an excample:
if (number ==10)
{Console.WriteLine ("You are 10");}
else if (number ==20)
{Console.WriteLine ("You are 20");}
else if (number ==30)
{Console.WriteLine ("You are 30");}
Else
{Console.WriteLine ("You are not 30");}
Above code can replaced as:
Switch (number)
{
Case:10
Console.WriteLine ("You are 10");
Break
Case:20
Console.WriteLine ("You are 20");
Break
Case:30
Console.WriteLine ("You are 30");
Break
Defalut:
Console.WriteLine ("You are not 30");
}
Or can be replaced as:
Switch (number)
{
Case:10
Case:20
Case:30
Console.WriteLine ("You Number is{0}", number);
Default
Console.WriteLine ("You are not 30");
}
noted that switch (expression), expression value must is bool, char, string, int, enum, or nullable type.
Session when loop in C #
1,while Loop checks the condition first.
2,if the condition is true,statements with in the loop is executed.
3,this process is repeated as long as the condition evaluetes to true.
Note:don ' t forget to update the variable participating in the Condition,so the loop can end, at some point
Session and Loop in C #
1,a Do loop checks it condition at the end of the loop.
2,this means that does loop is guranteed-to-execute at least one time.
3,do loops is used to present a menu to the user
Difference-while & Do While
1,while Loop checks the condition at the beginning, where as does while loops checks the condition at the end of the loop.
2,do loop is guaranteed-to-execute at least once, where as while loop was not
C # for beginner session ten to 20