Does the sequence of C statements affect the results of the program

  • 2021-08-17 00:49:08
  • OfStack

Below through a piece of code for everyone to parse C # statements in different order, the results are not 1.


using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 namespace Test
 {
  /// <summary>
  ///  Custom class, encapsulating addend and addend properties 
  /// </summary>
  class MyClass
  {
   private int x = ;      // Definition int Variable of type, as addend 
   private int y = ;      // Definition int Variable of type, as addend 
   /// <summary>
   ///  Additive 
   /// </summary>
   public int X
   {
    get
    {
     return x;
    }
    set
    {
     x = value;
    }
   }
   /// <summary>
   ///  Additive 
   /// </summary>
   public int Y
   {
    get
    {
     return y;
    }
    set
    {
     y = value;
    }
   }
   /// <summary>
   ///  Summation 
   /// </summary>
   /// <returns> Additive operation sum </returns>
   public int Add()
   {
    return X + Y;
   }
  }
  class Program
  {
   static void Main(string[] args)
   {
    MyClass myclass = new MyClass(); // Instantiation MyClass Object of 
    myclass.X = ;     // For MyClass Property assignment in class 
    myclass.Y = ;     // For MyClass Property assignment in class 
    int kg = myclass.Add();
    Console.WriteLine(kg); // Call MyClass Class in the Add Method summation 
    Console.ReadLine();
   }
  }
 }

If the statement in line 60 is put into line 56, the result output is 0 instead of 8. Therefore, when designing the program, we should pay attention to the order of statements and have clear thinking logic.

There is still a little time, and then I will introduce you to the summary of loop statements in C #

You can create a loop by using a loop statement. The loop statement causes the embedded statement to execute multiple times according to the loop termination condition. Unless jump statements are encountered, these statements will be executed in sequence.

The following keywords are used in the C # loop statement:

do... while

· ES 19EN

foreach... in

· while

do...while

do... while statement executes 1 statement or repeats until the specified expression evaluates to false, the body of the loop must be enclosed in curly braces {}, and the while condition ends with a semicolon

Example

The following example implements the execution of an do-while loop statement


public class TestDoWhile 
{
 static void Main () 
 {
  int x = 0;
  do 
  {
    Console.WriteLine(x);
    x++;
  } while (x < 10);
 }
}
/*

Output:

0
1
2
3
4
5
6
7
8
9
*/

The do-while loop executes once before the conditional expression is evaluated, and if the while expression evaluates to true, execution continues to loop in the first statement. If the expression evaluates to false, execution continues from the first statement after the do-while loop.

The do-while loop can also exit with an break, goto, return, or throw statement.

for

The for loop repeats 1 statement or block of statements until the specified expression evaluates to an false value. for loops are useful for circular arrays and sequential processing.

Example

In the following example, the value of int i is written to the console, and i is incremented by 1 each time it passes through the loop.


class ForTest 
{
 static void Main() 
 {
  for (int i = 1; i <= 10; i++)
  {
   Console.WriteLine(i);
  }
 }
}
/*

Output:

1
2
3
4
5
6
7
8
9
10
*/

The for statement repeats the enclosed statement as follows:

First, calculate the initial value of the 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 Console. WriteLine statement is executed and i is recalculated.

When i is greater than 10, the condition becomes false and control is passed out of the loop.

The for statement may be executed zero or more times because the test of the conditional expression occurs before the loop is executed. You can exit the loop by using an break, goto, throw, or return statement.

The for statement for all expressions is optional for example for the following statement to write an infinite loop.


for (; ; )
{
 // ...
}

foreach...in

The foreach statement is useful for implementing System. Collections. IEnumerable or System. Collections. Generic. IEnumerable < T > Repeats 1 set of embedded statements for each element in an array or collection of objects of the. The foreach statement is used to iterate through the collection to get the information you need, but it cannot be used to add or remove items from the source collection, which may have unpredictable side effects. If you need to add or remove items from the source collection, use the for loop.

The embedded statement continues execution for each element in the array or collection. When the loop is complete for all elements in the collection, control is passed to the next 1 statement after the foreach block.

You can use the break keyword to jump out of the loop at any point in the foreach block, or use the continue keyword to enter the next round of the loop.

The foreach loop can also exit with an break, goto, return, or throw statement.

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)
   {
    System.Console.WriteLine(i);
   }
  }
 }
 /*

Output:

0
1
2
3
4
5
*/

while

The while statement executes 1 statement or block of statements until the specified expression evaluates to false.


 class WhileTest 
 {
  static void Main() 
  {
   int n = 1;
   while (n < 5) 
   {
    Console.WriteLine(n);
    n++;
   }
  }
 }
 /*

Output:

1
2
3
4
*/


Related articles: