A loop in C # executes a while loop

Source: Internet
Author: User

There are three main ways in which code is executed in programming. (1) Sequential execution, that is, a statement of a statement executed sequentially, (2) conditional execution, that is, if...else. Executes some code when a condition is met, and (3) loops execution, which executes some code repeatedly when certain conditions are met. Many complex software applications are created by the magical combination of these three ways. We've already talked about sequential execution and conditional execution. In this exercise, we'll look at loop execution.

There are two types of loops that we often use in our daily work in C #. (1) while loop; (2) for loop; Of course, C # provides more than two ways to perform loops. Interested, can check on the Internet. Do not find, also want to know, remember to contact me oh.

1. While loop

while (test condition)

{

When the condition is true, the loops execute these statements in curly braces

}

When the condition is false, end the loop and execute the following statement

The execution of the Whille loop is like this.

1. First determine the test condition of the while and, if true, execute the statement in curly braces.

2. Execute the statement in curly braces, then judge the test condition of while, and if it is true, continue executing the statement in curly braces.

3. If False, end the while loop. Executes the following statement.

We have a better understanding of the while loop below through practical examples.

You create a project. The solution name is Sloution17, the project name is Exercise17, and the project type is a console program. Click Program.cs. Add the code to the inside.

/span>

Simply explain the program.

1. Line 17th i++; is an expression that increments I by 1 at a time, for example I first equals 1, runs i++, and I becomes 2 after the statement;

2. This program is the DOS window output I current value, and then add I 1. If I is not greater than 10, I continue to loop execution.

Run results

Practice

1. Read this code and annotate important statements

2. If the 17th line of code is dropped, what will be the result of running the program?

3. Modify the code to allow the program to output the value of I from 0 to 20 to the DOS window.

A loop in C # executes a while loop

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.