The following is a piece of code to parse C# statements with different orders and the execution results are different.
using System; using ; using ; using ; namespace Test { /// <summary> /// Custom class, encapsulate add and add the attributes /// </summary> class MyClass { private int x = ; //Define an int-type variable as an adder private int y = ; //Define an int variable as added /// <summary> /// Add /// </summary> public int X { get { return x; } set { x = value; } } /// <summary> /// Added /// </summary> public int Y { get { return y; } set { y = value; } } /// <summary> ///Sum /// </summary> /// <returns>Additional operations</returns> public int Add() { return X + Y; } } class Program { static void Main(string[] args) { MyClass myclass = new MyClass(); //Instantiate the object of MyClass = ; // Assign values to attributes in MyClass class = ; // Assign values to attributes in MyClass class int kg = (); (kg); //Calling the Add method in MyClass class to sum (); } } }
If the statement on line 60 is placed on line 56, the result output is 0 or not 8. Therefore, when designing the program, you should pay attention to the order of the statements and have clear thinking logic.
There is still some time left below, and I will introduce you to the summary of loop statements in C#
Loops can be created by using loop statements. Loop statements cause embedded statements to be executed multiple times according to loop termination conditions. Unless a jump statement is encountered, these statements will be executed in order.
The following keywords are used in C# loop statements:
· do...while
· for
· foreach...in
· while
do...while
The do...while statement executes a statement or statement repeats until the specified expression evaluates to false. The body of the loop must be enclosed in braces {}, and the while condition ends with a semicolon.
Example
The following example implements the execution of a do-while loop statement
public class TestDoWhile { static void Main () { int x = 0; do { (x); x++; } while (x < 10); } } /*
Output:
0
1
2
3
4
5
6
7
8
9
*/
The do-while loop will be executed once before calculating the conditional expression. If the while expression evaluates to true, the execution will continue to loop in the first statement. If the expression evaluates to false, execution will continue from the first statement after the do-while loop.
The do-while loop can also be exited through break, goto, return, or throw statements.
for
for loop repeatedly executes a statement or statement block until the specified expression is calculated as a false value. for loops are useful for looping arrays and sequential processing.
Example
In the following example, the value of int i will be written to the console and i is added 1 each time it passes through the loop.
class ForTest { static void Main() { for (int i = 1; i <= 10; i++) { (i); } } } /*
Output:
1
2
3
4
5
6
7
8
9
10
*/
The for statement repeatedly executes enclosed statements, as follows:
· First, calculate the initial value of variable i.
· Then, as long as the value of i is less than or equal to 10, the conditional calculation result is true. At this point, the statement is executed and i is recalculated.
· When i is greater than 10, the condition becomes false and the control is passed outside the loop.
Since the test of a conditional expression occurs before the loop is executed, the for statement may be executed zero or more times. The loop can be exited by using break, goto, throw, or return statements.
The for statements of all expressions are optional, for example, for the following statements to be used to write an infinite loop.
for (; ; ) { // ... }
foreach...in
The foreach statement repeats a set of embedded statements for each element in an array or object collection that implements or <T> interfaces. The foreach statement is used to iterate over the collection to get the information you need, but it cannot be used to add or remove items from the source collection, otherwise unpredictable side effects may occur. If you need to add or remove items from the source collection, use a for loop.
The embed statement continues to execute as each element in the array or collection. When the loop is completed for all elements in the set, control is passed to the next statement after the foreach block.
You can use the break keyword to break out of the loop at any point in the foreach block, or use the continue keyword to enter the next loop of the loop.
The foreach loop can also be exited through break, goto, return, or throw statements.
Example
In this example, foreach is used to display the contents of an array of integers.
class ForEachTest { static void Main(string[] args) { int[] barray = new int[] { 0, 1, 2, 3, 4, 5}; foreach (int i in barray) { (i); } } } /*
Output:
0
1
2
3
4
5
*/
while
The while statement executes a statement or statement block until the specified expression is calculated as false.
class WhileTest { static void Main() { int n = 1; while (n < 5) { (n); n++; } } } /*
Output:
1
2
3
4
*/