Methods for the For loop, while loop execution in C # _c# tutorial

Source: Internet
Author: User

Let me introduce you to the loop execution for loop in C #

In this practice, we'll show you another important circular statement in C #, for loops.

for (expression 1; expression 2; expression 3) 
{ 
loop-body 
}

Expression 1: Generally the assignment expression, gives the control variable an initial value;

Expression 2: Logical expression, loop control condition, when the condition is true, loop executes the statement in the loop body.

Expression 3: Typically an assignment expression that gives the control variable an increment or decrement.

This does not seem to be quite abstract. Through the actual example, you will be very easy to understand for the loop.

You create a project. The solution name is Sloution18, the project name is Exercise18, and the project type is a console program. Click Program.cs to add the code to the inside.

Explain this program briefly.

1. Line 14th is the core of the For loop. Where I is the control variable. int i=0; Give the control variable i an initial value;

2. i<=10; is a cyclic control condition; When i<10, execute the statement inside the curly braces;

3. i++ is the control variable increment. Plus 1 each time.

4. Line 17th is an accumulation of 1 to 10. is to calculate 1+2+3+4+ ... +10.

Run results


Practice

1. Read this code and annotate important statements

2. If the 14th line of code is changed to for (int i=0;i<=10;) What will be the result of running the program?

3. If the 14th line of code is changed to for (int i=0;;; i++) What will be the result of running the program?

4. Modify the code to allow the program to output the value of I from 0 to 20 to the DOS window, while calculating the cumulative result of 0 to 20.

There are three main ways in which code is executed in programming. (1) Sequential execution, that is, a statement a statement is executed sequentially, (2) The condition is executed, namely If...else. Execute some code when certain conditions are met, (3) iterate, that is, to execute some code repeatedly when certain conditions are met. Many complex software applications are created by the magical combination of these three ways. We've talked about sequential execution and conditional execution before. In this section of the exercise, we need to learn about circular execution.

Loop execution while loop in C #

There are two types of loops that we often use in C # in our daily work. (1) while loop; (2) for loop; Of course, C # provides more than two ways to execute loops. Interested, you can check on the Internet. I can't find it, I want to know it, please contact me.

1. While loop

while (test condition) 
{
When the condition is true, loop through the statements in the curly braces 
}

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

The Whille loop is executed in this way.

1. First judge the test condition while, if true, execute the statement in the curly braces.

2. Execute the statements in the curly braces, then judge the test conditions while, if true, continue with the statements in the curly braces.

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

We have a better understanding of the while loop with the actual example below.

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 to add the code to the inside.

Explain this program briefly.

1. Line 17th i++; is an expression that indicates that I was increased by 1 each time, for example I first equals 1; When I run i++, I become 2.

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

Run results


Practice

1. Read this code and annotate important statements

2. What would be the result of running a program if the 17th line of code was injected?

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

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.