1.17. C # for/foreach loop

发布时间 : 2025-10-25 13:34:40 UTC      

Page Views: 9 views

One for loop is a repetitive control structure that allows you to writea loop that executes a specific number of times.

1.17.1. Grammar #

In C # for syntax of the loop:

for ( init; condition; increment ) { statement(s); } 

The following is for control flow of the loop:

  1. init will be executed first, and only once. This step allows you to declare and initialize any loop control variables. You can also not write any statements here, as long as a semicolon appears.

  2. Next, it will judge condition . If true, the loop body is executed. If false, the loop body is not executed, and the control flow jumps to the following for statement of the loop.

  3. After the execution for after looping the body, the control flow jumps back to the increment statement. This statement allows you to update loop control variables. This statement can be left blank as long as a semicolon appears after the condition.

  4. The conditions are judged again. If true, the loop is executed, and the process is repeated over and over again (loop body, then increase the step value, and then re-determine the condition). When the condition becomes false for cycle ends.

1.17.2. Flow chart #

For loop in C #

1.17.3. Example #

using System; namespace Loops { class Program { static void Main(string[] args) { /* for loop execution */ for (int a = 10; a < 20; a = a + 1) { Console.WriteLine("Value of a: {0}", a); } Console.ReadLine(); } } } 

When the above code is compiled and executed, it produces the following results:

Value of a: 10 Value of a: 11 Value of a: 12 Value of a: 13 Value of a: 14 Value of a: 15 Value of a: 16 Value of a: 17 Value of a: 18 Value of a: 19 

1.17.4. Foreach #

C # also supports foreach looping, use foreach , you can iterate through an array or a collection object.

The following example has three parts:

  • Pass through foreach loop outputs elements in an integer array.

  • Pass through for loop outputs elements in an integer array.

  • foreach loops to set the calculator for array elements.

class ForEachTest { static void Main(string[] args) { int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 }; foreach (int element in fibarray) { System.Console.WriteLine(element); } System.Console.WriteLine(); // Similar to foreach loop for (int i = 0; i < fibarray.Length; i++) { System.Console.WriteLine(fibarray[i]); } System.Console.WriteLine(); // Set the calculator for elements in the collection int count = 0; foreach (int element in fibarray) { count += 1; System.Console.WriteLine("Element #{0}: {1}", count, element); } System.Console.WriteLine("Number of elements in the array: {0}", count); } } 

The output is as follows:

0 1 1 2 3 5 8 13 0 1 1 2 3 5 8 13 Element #1: 0 Element #2: 1 Element #3: 1 Element #4: 2 Element #5: 3 Element #6: 5 Element #7: 8 Element #8: 13 Number of elements in the array: 8 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。