c#_basic knowledge 3.0

1. Loop statement

The loop statement is a more important statement in c#.
What is a loop statement?
A loop statement is a basic loop that loops if the loop condition is met, and jumps out of the loop if it is not satisfied. The loop statement solves the problem that the statement is executed only once under the if condition.
Several kinds of loop statements are listed below:
(1). while loop
while (loop condition) is a basic loop, where () is the loop condition and the statement under the loop is executed when the loop condition is true.
For example, the following is the sum of the first 20 numbers:

using System;
namespace A
{
    
    
	class B
	{
    
    		
	   static void Main(string[] args)
	   {
    
    
	   		int i=1;
	   		while(i<=20)
	   		{
    
    
	   		 s=s+i;
	   		 i++;
	   	    }
	   	   Console.WriteLine("前20个数的总和是:{0}",s);
	   	     Console.ReadKey();
	    }
	}
}

turn out:

20个数的总和是:210

(2).do-while loop
do-while is similar to while but different from while in that the do-while statement is to execute the statement in the loop before making a judgment, so do-while must execute the loop at least once. like:

using System;
namespace B
{
    
    class C
{
    
    
	static void Main(string[] args)
	{
    
        int a;
	    do
	    {
    
    
	    a=1;
	    s=s+a;
	    a++;
	    }
	    while(a<=20)
	    Console.WriteLine("总值是{0},a的数值为{1}",s,a);
	    Console.ReadKey();
	}
}
}

turn out:

总值是210,a的数值为21

(3). The for loop
is the most convenient loop statement for the for statement in C#.

The general form is for (expression 1; expression 2; expression 3) Expression 1 is the assignment of the loop variable. Such as: i=0, 1, 2....
Expression 2 is to judge the loop condition. If it is true, the loop statement will continue to execute. If it is false, it will jump out of the loop body. Expression 3 will increase the loop variable.

Use for to write the sum of the first 20 numbers:

using System;
namespace B
{
    
    
class C
 {
    
    
    static void Main(string[] args)
    {
    
    
    int i,j;
    for(i=1;i<=20;i++)
    {
    
    
       s=i+s;
    }
    Console.WriteLine("总和是:{0}",s);
    Console.ReadKey();
    }
 }
}

turn out:

总和是:210

2. Loop nesting

After learning the loop, you can learn the nesting of the most important part of the loop.
As the name implies, the nesting of loops is to nest another loop inside the loop, which is a double loop. If another loop is nested inside, it is a triple loop. The double loop haswhlie-whlietype,while-fortype,for-fortype etc.

while(表达式)						while(表达式)
{
    
    ...										{
    
       ......
  while(表达式)							for(表达式1;表达式2;表达式3)
    {
    
    										       {
    
    
      ....													......
    }												}
}											}
for(表达式1;表达式2;表达式3)
{
    
    
    ......
    for(表达式1;表达式2;表达式3)
    {
    
    
       ......
    }
}

The double loop first executes the outermost loop and then executes the inner loop, and only returns to the outer loop after the inner loop is executed, and then executes the inner loop, and so on until the outer loop is executed, and finally outputs the result .
The following is a ninety-nine percent formula written with a double for loop

using System;
namespace chengfabiao
{
    
    
	class biao
	{
    
    
	static void Main(string[] args)
	{
    
    
		int i,j;
		for(i=1;i<=9;i++)
		{
    
    
		  for(j=1;j<=i;j++)
		   {
    
    
		     s=i*j;
		     Console.Write("{0}*{1}={2} ",i,j,s);
		   }
		   Console.Writeline();
		}
		   Console.ReadKey();
	}
	} 
}

turn out:

1*1=1
1*2=2  2*2=4 
1*3=3  2*3=6  3*3=9 
1*4=4  2*4=8  3*4=12 4*4=16
1*5=5  2*5=10 3*5=15 4*5=20 5*5=25
1*6=6  2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7  2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8  2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9  2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

3. Type conversion

Type conversion is divided into explicit conversion and implicit conversion.
(1). Implicit conversion
Implicit conversion is low-level to high-level conversion, such as conversion from int type to double type. Implicit conversion does not require special instructions but is automatically completed by the compiler.

int a = 10;
double b = a;//隐式转换

The implicit conversion rule is: Any type A can be implicitly converted to type B as long as its value range is completely contained in the value range of type B. Based on this conversion rule, C#'s implicit conversion will not cause data loss. It should be noted that our most commonly used simple types bool and string have no implicit conversion.

(2).
Explicit conversion: The conversion from type A to type B can only be performed in some cases, the conversion rules are more complicated, and some type of additional processing should be performed. Explicit conversion is also called forced type conversion. Explicit conversion requires the user to explicitly specify the conversion type. Such as converting double type data to int type data.
For example:

double c = 10.5;
int d = (int)c;//显示转换

Notice:

Explicit conversions may result in errors. The compiler will perform overflow detection on the conversion when this conversion is performed. If there is an overflow indicating that the conversion failed, it means that the source type is not a valid target type. Cannot perform type conversion.
Mandatory type conversion will cause data loss. For example, in the above example, the final d value is 10.

4. Encapsulation

In C#, enclosing one or more projects in a package prevents external access to project details. Encapsulation restricts external access rights. Encapsulation can be implemented with access modifiers.
Following are the access modifiers and levels:

Levels increase from outside to inside
The image above shows increasing levels of access from outside to inside.
(1).private modifier

The Private
access modifier allows a class to hide its member variables and member functions from other functions and objects. Only functions in the same class can access its private members. Even an instance of a class cannot access its private members.

(2).protected modifier

The Protected access modifier allows a subclass to access member variables and member functions of its base class. This helps to implement inheritance

(3).internal modifier

The internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current program. In other words, any member with the internal
access modifier can be accessed by any class or method defined within the application in which the member is defined

(4).public modifier

The Public access modifier allows a class to expose its member variables and member functions to other functions and objects. Any public members can be accessed by external classes.

Note:In addition to encapsulating with the above modifiers, you can also use get and set, where get is readable and set is writable.

Guess you like

Origin blog.csdn.net/AD_GOD/article/details/121299770