Dark Horse programmer _ Use of loop statements

Source: Internet
Author: User

Use of loop statements

Loop, as its name implies, is repetition. in C #, it is repeated to do something, that is, to execute a piece of code. in C #, there are only three loop structures: while loop, do while loop, and for loop; in fact, the functions are the same, and they can be converted to each other. Note that in terms of the writing format and various cycles, different from each other.

1. While Loop

As I said at the beginning, the loop is somewhat similar to IF and else. Before execution, the loop must be implemented first. If the logic is determined and the loop is true, the loop will be executed; otherwise, the loop ends; remember the writing format here:

Eg :{

While (<cyclic condition> ){

// The statement executed when the condition is true, that is, the loop body.

// You must note that a variable must exist in the loop body.

// Keep changing, and eventually make the cycle condition not true, so that the cycle ends,

// Otherwise it will become an endless loop

}

}

The condition of a loop is a logical expression. If it is true, the loop is executed. Otherwise, the loop ends;

The while loop is mainly used. I don't know how many times it will be recycled;

There must be a loop increment in the loop body to make the loop end, otherwise it is an endless loop;

When designing a program, we should try to avoid endless loops, but some programs also need to use endless loops;

2. Do WHILE LOOP

The do while loop and the while loop are reversed. The latter judges the condition first and then executes the loop body. The former is opposite, regardless of the, the first loop body is executed, then, when the condition is determined, the loop will be executed at least once. Don't look at this subtle difference. You need to know that sometimes the while loop may not be executed at one time, because the initial condition may not be true;

Writing format:

{

Do {

// The loop body is placed here, which is the same as while.

} While (<cyclic condition>); // note that there is a semicolon, which cannot be used.

// Compare while to remember. If the semicolon is missing, compilation will fail.

 

}

3. For Loop

The for loop is used more frequently in programming than the other two loops, probably because of its writing format, it makes the loop conditions, body, and increment of the loop appear clear at a glance, and it is not easy to create an endless loop. Sometimes while and do while may inadvertently forget to write the loop increment, the loop cannot end;

Writing format:

{

For (<initialization variable>; <cycle condition>; <loop increment (also called iterator)> ){

// Loop body

}

}

The three parameters in for, the initialization variable, and the iterator can ignore or not write;

As mentioned just now, the three types of loops can be converted to each other, but they only focus on the types of loops. The for loop mainly focuses on the number of loops and is opposite to the while loop;

Its execution process is only slightly different from the previous two cycles, that is, after the loop body is executed, an iterator is executed and then the conditions of the loop are determined. All others are the same;

4. Loop other end Methods

A new problem is raised here, except that the cycle condition is not true and the loop ends. Is there any other way to end the loop?

The answer is yes, and there are many methods, but several keywords are involved. Their usage is as follows:

  • Break: it is the same as the break in case. Once this keyword is executed in a loop, the loop ends immediately and is very simple. The function is to jump out of the current loop;
  • Continue: Unlike the Clean exit loop of break, continue is more euphemistic. When it is encountered in the loop, the program will skip this loop, which is the current loop, then, judge the condition of the loop and execute the next loop;
  • Goto: its use is controversial. If it is used too much, it will lead to program confusion. This is also because its function is too powerful; it is encountered in the loop body, it will jump to the tag specified by it (that is, the tag marked in the code in advance );
  • Return: It is much simpler and more crude. Like break, it is also a direct exit loop, but it exits more severely and exits the function directly.

When the above four keywords appear in the loop, they cannot be followed by other code, so the compiler will report an error because the Code cannot be executed at all;

How to tag in the Code: The tag can only be placed in front of the selection and loop structure. It acts like a shortcut on the desktop. When this label is used, the program directly jumps to the code corresponding to the tag:

Writing format:

{

Outer: For () {// This is a tag before the loop

Inner: For () {// This is also a tag;

If (){

// Note that labels can be used for break, continue, and Goto,

// Their original functions remain unchanged

Break outer; // jump to the outer tag and execute the corresponding code;

}

}

}

}

A nested for loop is defined here, and there is a judgment condition in the loop. Once the condition is true, it immediately jumps to the for loop outside, which interferes with the process, the normal execution of the program to achieve the functions we need;

Dark Horse programmer _ Use of loop statements

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.