Flexible use of several loops in C #

Source: Internet
Author: User

Learning the Web front end has been a week! From the Zero Foundation to the present, for some simple code writing and learning direction has a certain understanding, in this section of the study for code writing some simple loop effect is more profound, here do some simple comb.

Because you don't know what kind of cycle is more reasonable when you use it.

The most common for loop:

//for (expression 1; loop condition; expression 2) For9 ( int times=0;times<, times++)     {                       Console.WriteLine (" Hello " )     }

The For loop first runs expression 1, determines whether the loop body is true, if true, executes the loop body, executes in the run Expression 2, and then judges the loop body, knowing that the expression is false and the loop ends.

Where expression 1 · can be any code, will be executed, and will only be executed once, and expression 2 can be any type of code, each time the loop body execution must be executed.

While Loop:

while (loop condition) (value, variable, expression, but must be of type bool)
int times=o;while (times <) { Console.WriteLine (" Hello "); times + +; }

While loop if the loop condition is true, then the loop body is executed, after executing the loop body in judging whether the condition is true, if true, then in the execution of the loop body, and then in judgment, has been carried out, know that the loop condition is false, will end.

★ The first two code examples show the same effect for the while loop and for loop, but the for loop is more concise and understandable.

Do and loop:

Double   num;    do {              Console.Write ("Enter a number:");              num=double. Parse (Console.ReadLine ());    } while               (num<=0)               Console.WriteLine ( " the number you entered is "+"num");

The Do While loop executes the loop body first, then determines whether the loop body is satisfied, then executes the loop body again if satisfied, and in judgment, knows not satisfied, the program ends.

Do while loops are relatively small in their learning, but do not represent unimportant, as follows

Console.Write ("Please enter a number:"); double. num=Double. Parse (Console.ReadLine ()); while (num<0)         {               Console.Write (" Please enter a number:");               num=double. Parse (Console.ReadLine ());         }               Console.WriteLine (" The number you entered is "+ "num")

This code is the same as the previous do and loop example, but using the Do While loop is more concise and understandable.

Break and Continue

Break (1.switch immediately ends switch selection; 2 loop understanding end loop);

Continue: (Loop is immediately closed for the cycle of election)

This time to come here, write bad or have the wrong place hope everyone for me to correct, and then work together.

More study, more summary, more thinking, more connections, I believe that in the later study still need more efforts, so as to learn deeper knowledge, go farther.

Flexible use of several loops in C #

Related Article

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.