当前位置:网站首页>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 :
readonly
Initialize in a declaration or constructorconst
Initialize 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
break
sentence : Jump out of the innermost closed loop orswitch
In the sentencecontinue
sentence : 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 */
边栏推荐
- ROS安装报错 sudo: rosdep:找不到命令
- 5. 数据访问 - EntityFramework集成
- Is it safe to make fund fixed investment on access letter?
- Taishan Office Technology Lecture: from the layout height of the line, talk about the height overflow and height shrinkage of the drawing height (launched in the whole network)
- Isprs2020/ cloud detection: transferring deep learning models for cloud detection between landsat-8 and proba-v
- Benefits of automated testing
- EasyCVR授权到期页面无法登录,该如何解决?
- AI金榜题名时,MLPerf榜单的份量究竟有多重?
- c语言简便实现链表增删改查「建议收藏」
- What is text mining? "Suggested collection"
猜你喜欢
The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)
Overview of video self supervised learning
How to write good code defensive programming
[detailed explanation of AUTOSAR 14 startup process]
Technology sharing | interface testing value and system
Analysis of postman core functions - parameterization and test report
Isprs2020/ cloud detection: transferring deep learning models for cloud detection between landsat-8 and proba-v
How to automatically install pythn third-party libraries
5. 数据访问 - EntityFramework集成
Reading notes of Clickhouse principle analysis and Application Practice (5)
随机推荐
彻底理解为什么网络 I/O 会被阻塞?
图扑软件数字孪生智慧风电系统
2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】
The easycvr authorization expiration page cannot be logged in. How to solve it?
Icml2022 | partial and asymmetric comparative learning of out of distribution detection in long tail recognition
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
从外卖点单浅谈伪需求
2022 latest Android interview written examination, an Android programmer's interview experience
A cloud opens a new future of smart transportation
Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(下)
Powerful tool for collection processing
一朵云开启智慧交通新未来
The road of enterprise digital transformation starts from here
Is it safe to make fund fixed investment on access letter?
[detailed explanation of AUTOSAR 14 startup process]
Use file and directory properties and properties
EasyCVR电子地图中设备播放器loading样式的居中对齐优化
RedHat7.4配置yum软件仓库(RHEL7.4)
Optimization of middle alignment of loading style of device player in easycvr electronic map
潘多拉 IOT 开发板学习(HAL 库)—— 实验8 定时器中断实验(学习笔记)