首页 > 代码库 > C# for Beginner session 10 to 20

C# for Beginner session 10 to 20

Session 11-12 switch statement in C#

switch statement

break statement   if break statement is used inside a switch statement,he control will leave the           switch statement

goto statement    you can either jump to another case statement,or to specific label(label is some          word you coding 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 number is 10");}

else if(number ==20)

{Console.WriteLine("you number is 20");}

else if(number ==30)

{Console.WriteLine("you number is 30");}

else 

{Console.WriteLine("you number is not 10 to 30");}

above code can be replaced as:

switch(number)

{

  case :10 

  Console.WriteLine("you number is 10");

  break;

  case :20 

  Console.WriteLine("you number is 20");

  break;

     case :30 

  Console.WriteLine("you number is 30");

  break;

      defalut:

  Console.WriteLine("you number is not 10 to 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 number is not 10 to 30");

}

Noted that switch(expression) , expression value must be bool , char, string, int ,enum or nullable type.

Session 13 while loop in c#

1,while loop checks the condition first.

2,if the condition is true,statements with in the loop are 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 14 while loop in c#

1,a do loop checks its condition at the end of the loop.

2,this means that the do loop is guranteed to execute at least one time.

3,do loops are used to present a menu to the user

Difference -while & do while

1,while loop checks the condition at the beginning, where as do while loop checks the condition at the end of the loop.

2,do loop is guaranteed to execute at least once, where as while loop is not

C# for Beginner session 10 to 20