C # for beginner session ten to 20

Source: Internet
Author: User
Tags case statement

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.