当前位置:网站首页>The basic grammatical structure of C language
The basic grammatical structure of C language
2022-07-05 19:04:00 【Youth programming competition communication】
C# The basic grammatical structure of language
The knowledge structure :

1、 data type
The first category :
- Simple data type :
byte、short、int、long、float、double、char、bool - Combined data types :
struct、enum、class、interface
| type | describe |
|---|---|
| byte | Unsigned 8 An integer |
| (ushort) short | ( nothing ) A signed 16 An integer |
| (uint) int | ( nothing ) A signed 32 An integer |
| (ulong) long | ( nothing ) A signed 64 An integer |
| float | 32 Bit floating point type |
| double | 64 Bit floating point type |
| char | 16 position unicode character ( International standard character set ) |
| bool | Boolean type |
The second category :
- Value type : When passed as a parameter , Deliver copy .
- Include : Simple data type 、struct type 、enum type
- Reference type : When passed as a parameter , Delivery address .
- Include :class type 、 Array
example 1:
public struct Book
{
public double Price;
public string Title;
public string Author;
}
class Program
{
static void ChangeBook(Book bk)
{
bk.Price = 1.01;
bk.Title = "Spss";
bk.Author = "John";
}
static void PrintBook(Book bk)
{
Console.WriteLine("Book Infor:\n Price={0},Title={1},Author={2}",
bk.Price, bk.Title, bk.Author);
}
static void Main(string[] args)
{
Book bk;// = new Book();
bk.Price = 10.01;
bk.Title = "MatLab";
bk.Author = "Tom";
PrintBook(bk);
ChangeBook(bk);
PrintBook(bk);
//Book Infor:
//Price=10.01,Tile=MatLab,Author=Tom
//Book Infor:
//Price=10.01,Tile=MatLab,Author=Tom
}
}
This example shows “ Value type ” Deliver copy , Do not change the value stored by itself .
example 2:
public class Book
{
public double Price;
public string Title;
public string Author;
}
class Program
{
static void ChangeBook(Book bk)
{
bk.Price = 1.01;
bk.Title = "Spss";
bk.Author = "John";
}
static void PrintBook(Book bk)
{
Console.WriteLine("Book Infor:\n Price={0}, Tile={1}, Author={2}",
bk.Price, bk.Title, bk.Author);
}
static void Main(string[] args)
{
Book bk = new Book(); // error Book bk;
bk.Price = 10.01;
bk.Title = "MatLab";
bk.Author = "Tom";
PrintBook(bk);
// Book Infor:
// Price = 10.01, Tile = MatLab, Author = Tom
ChangeBook(bk);
PrintBook(bk);
// Book Infor:
// Price = 1.01, Tile = Spss, Author = John
}
}
example 3:
class Program
{
static void ChangeArrayItem(int[] array)
{
for (int i = array.Length - 1; i >= 0; i--)
{
array[i] = array.Length - 1 - i;
}
}
static void PrintArrayItem(int[] arry)
{
for (int i = 0; i < arry.Length; i++)
{
Console.Write("{0} ", arry[i]);
}
Console.WriteLine();
}
static void Main(string[] args)
{
int[] arr = new int[3];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = i;
}
PrintArrayItem(arr); // 0 1 2
ChangeArrayItem(arr);
PrintArrayItem(arr); // 2 1 0
}
}
example 2, example 3 explain “ Reference type ” Delivery address , To change the value stored by itself . In specific application , it is to be noted that “ Value type ” and “ Reference type ” The difference between .
2、 Variables and constants
- Variable definitions :
Variable type Variable name ; - Constant definition :
readonlyInitialize in a declaration or constructorconstInitialize on declaration
example 4:
public class SimpleClass
{
public int X;
public readonly int Y = 2;
public readonly int Z;
public const double Pi = 3.1415926;
public const string Etc = "...";
public SimpleClass()
{
Z = 3;
}
public SimpleClass(int p1, int p2, int p3)
{
X = p1;
Y = p2;
Z = p3;
}
}
class Program
{
static void Main(string[] args)
{
SimpleClass sp1 = new SimpleClass();
sp1.X = 1;
Console.WriteLine("sp1:x={0}, y={1}, z={2}", sp1.X, sp1.Y, sp1.Z);
// sp1: x = 1, y = 2, z = 3
SimpleClass sp2 = new SimpleClass(-1, -2, -3);
Console.WriteLine("sp2:x={0} ,y={1}, z={2}", sp2.X, sp2.Y, sp2.Z);
// sp2: x = -1 ,y = -2, z = -3
Console.WriteLine("PI={0}{1}", SimpleClass.Pi, SimpleClass.Etc);
// PI = 3.1415926...
}
}
Attention to the above examples readonly And const Define a constant and the difference when using it .
3、 Operators and expressions
Operator :
- Unary operator
x++,y++ - Binary operator
x+y,x-y - Ternary operator
max = (x>y)?x:y;
Operator :
- Arithmetic operator
+、-、*、/、% - Relational operator
>、>=、==、!=、<=、< - Logical operators
!、&&、||
expression : An expression consisting of an operator and a variable or constant .
4、 Basic statement
4.1 Assignment statement
Variable name = expression ;
4.2 Conditional statements
The first one is :
if( Conditional expression )
{
Statement sequence ;
}
The second kind :
if( Conditional expression )
{
Statement sequence ;
}
else
{
Statement sequence ;
}
The third kind of :
if( Conditional expression 1)
{
Statement sequence 1;
}
else if( Conditional expression 2)
{
Statement sequence 2;
}
else if( Conditional expression N)
{
Statement sequence N;
}
else
{
Statement sequence N+1;
}
4.3 Switch statement
swith( expression )
{
case value 1: Statement sequence 1; break;
case value 2: Statement sequence 2; break;
case value N: Statement sequence N; break;
default: Statement sequence N+1; break;
}
example 5:
class Program
{
static void Main(string[] args)
{
Random rdm = new Random();
int i = rdm.Next(1, 5);
Console.WriteLine(i); // 1
switch (i)
{
case 1:
Console.WriteLine("Case 1.");
break;
case 2:
Console.WriteLine("Case 2.");
break;
case 3:
Console.WriteLine("Case 3.");
break;
default:
Console.WriteLine("Default Case.");
break;
}
// Case 1.
i = rdm.Next(1, 5);// 1
Console.WriteLine(i);
switch (i)
{
case 1:
case 2:
case 3:
Console.WriteLine("It's 1,2 or 3.");
break;
default:
Console.WriteLine("Not Sure What it is.");
break;
}
//It's 1,2 or 3.
}
}
Attention to the above examples switch The grammatical structure of a sentence , Especially every one case Statements need to match break sentence .
4.4 Loop statement
The first one is :
for( Initialize the loop counter expression ; Determine the end condition of the cycle ; Increment or decrement loop counter expression )
{
Statement sequence ;
}
The second kind :
while( Conditional expression )
{
Statement sequence ;
}
The third kind of :
do
{
Statement sequence
}while( Conditional expression );
A fourth :
foreach( Element type Elements in aggregate )
{
Statement sequence ;// It is usually used to traverse every element in a collection
}
example 6:
class Program
{
static void Main(string[] args)
{
int i;
int sum = 0;
for (i = 1; i <= 10; i++)
{
sum += i;
}
Console.WriteLine(sum);// 55
sum = 0;
i = 1;
while (i <= 10)
{
sum += i;
i++;
}
Console.WriteLine(sum);// 55
sum = 0;
i = 1;
do
{
sum += i;
i++;
} while (i <= 10);
Console.WriteLine(sum);// 55
}
}
example 7:
class Program
{
static void Main(string[] args)
{
int[] arry = new int[] {
1, 3, 5, 7 };
foreach (int i in arry)
{
Console.WriteLine(i);
}
// 1
// 3
// 5
// 7
}
}
4.5 try…catch…finally sentence
try
{
Statement sequence ;
}
catch(Exception ex)
{
Statement sequence ;
}
finally
{
Statement sequence ;
}
example 8: Enter an integer on the screen , Then the integer will be displayed on the screen “*” Number .
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Please enter an integer :");
// Please enter an integer :
// abc
string sTemp = Console.ReadLine();
try
{
int iCount = int.Parse(sTemp);
for (int i = 0; i < iCount; i++)
{
Console.Write("*");
}
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine(" The reason for the mistake is :" + ex.Message);
// The reason for the mistake is : The input string is not in the correct format .
}
finally
{
Console.WriteLine(" end .");
// end .
}
}
}
Attention to the above examples try…catch…finally The grammatical structure of a sentence , This statement is usually used to catch and handle exceptions .
4.6 break、continue sentence
breaksentence : Jump out of the innermost closed loop orswitchIn the sentencecontinuesentence : Pass control to the next iteration of the closed loop
example 9:
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
{
if (i == 5)
break;
Console.WriteLine(i);
}
// 1
// 2
// 3
// 4
for (int i = 1; i <= 100; i++)
{
if (i < 99)
continue;
Console.WriteLine(i);
}
// 99
// 100
}
}
4.7 Comment statement
- Single-line comments :
// Text sequence - Multiline comment :
/* Text sequence */
边栏推荐
- Blue sky drawing bed Apple quick instructions
- Golang through pointer for Range implements the change of the value of the element in the slice
- How much does the mlperf list weigh when AI is named?
- Technology sharing | interface testing value and system
- Reading notes of Clickhouse principle analysis and Application Practice (5)
- Linear table - abstract data type
- Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
- A cloud opens a new future of smart transportation
- 决策树与随机森林
- Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
猜你喜欢
![2022 latest intermediate and advanced Android interview questions, [principle + practice + Video + source code]](/img/c9/f4ab4578029cf043155a5811a64489.png)
2022 latest intermediate and advanced Android interview questions, [principle + practice + Video + source code]

How to automatically install pythn third-party libraries

图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统

2022最新大厂Android面试真题解析,Android开发必会技术

Isprs2022/ cloud detection: cloud detection with boundary nets

【Autosar 十四 启动流程详解】

How much does the mlperf list weigh when AI is named?
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day

解决 contents have differences only in line separators

2022最新中高级Android面试题目,【原理+实战+视频+源码】
随机推荐
R语言使用lubridate包处理日期和时间数据实战
彻底理解为什么网络 I/O 会被阻塞?
RedHat7.4配置yum软件仓库(RHEL7.4)
Interprocess communication (IPC): shared memory
c期末复习
MySQL数据库索引教程(超详细)
Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
尚硅谷尚优选项目教程发布
CF: B. almost Ternary Matrix [symétrie + règles de recherche + Construction + I am Construction Waste]
蚂蚁集团开源可信隐私计算框架「隐语」:开放、通用
图扑软件数字孪生智慧风电系统
鱼和熊掌可以兼得!天翼云弹性裸金属一招鲜!
【Autosar 十四 启动流程详解】
A cloud opens a new future of smart transportation
android中常见的面试题,2022金九银十Android大厂面试题来袭
The easycvr authorization expiration page cannot be logged in. How to solve it?
Precautions for RTD temperature measurement of max31865 module
中文版Postman?功能真心强大!
Idea configuring NPM startup
为什么 BI 软件都搞不定关联分析?带你分析分析