2017.02.24c# Jump Statement, iterative method, exhaustive method, exception statement processing.

Source: Internet
Author: User
Tags try catch

One, jump statement
(1) Break:

Code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespace@break7 {8     class Program9     {Ten         Static voidMain (string[] args) One         { AInt32 A =0; -                  for(a=0;a< -; a++) -                 { the Console.WriteLine (a); -                     if(a==Ten) -                          -                     Break; +                      -                      +                 } A  at console.readline (); -         } -     } -}

If there is no break, the code from 11 straight output to 999, but broke the appearance of interrupted, when the a=10, out of the output, to 10 interrupted, only output to the 10,break function is to jump out of the area of the loop, the effect of the cycle of the area.


(2) Continue
Code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespace@break7 {8     class Program9     {Ten         Static voidMain (string[] args) One         { AInt32 A =0; -                  for(a=0;a< -; a++) -                 { the                      -                     if(a==Ten) -                    -                         Continue; + Console.WriteLine (a); -                 } +  A console.readline (); at         } -     } -}

The result of this code output is 0---24, there is No 10 in the middle, 10 when the time out of the output 10, but also continue to output 11 cycle, so the output does not have 10.continue effect is when this situation occurs, jump out of this cycle, and continue the next loop.


Second, the poor lifting method
Code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespacePoor Lifting Method7 {8     class Program9     {Ten         Static voidMain (string[] args) One{//100 yuan, gloves 5 yuan, socks 2 yuan, a total of several cases AInt32 A =0; -Int32 B =0; -              for(A =0; A <= -; a++) the             { -                  for(b =0; b <= -; b++) -                 { -                     if((A *5) + (b *2) == -) +                     { -Console.WriteLine ("can buy"+ A +"Gloves only,"+ B +"socks only. "); +                     } A  at                 } -             } -  - console.readline (); -  -  in  -  to  +  -  the         } *     } $}

This code prints out how many socks and gloves to buy and what the difference is.
The method of exhaustive is to enumerate the number of cases.

Third, exception statement processing

Try Catch


Code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceJob7 {8     class Program9     {Ten         Static voidMain (string[] args) One         { A              for (; ; ) -{Console.WriteLine ("Please enter a number"); -                 theString b=console.readline (); -                Try -                { -Int32 C =Convert.ToInt32 (b); +                     -Console.WriteLine ("I entered a number, and this time I'll spare you."); +                     Break; A                } at                Catch -                { -Console.WriteLine ("input is not a number, re-enter! "); -                } -  -             } in console.readline (); -         } to     } +}

This code means that the user enters the number, enter other prompt error, let the user re-enter, until the user entered the correct number to end the loop.
The try{code may or may not be correct, and if correct, continue running if the error runs catch}
Catch{try Error running here}
Finally{either right or wrong will run here}

2017.02.24c# Jump Statement, iterative method, exhaustive method, exception statement processing.

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.