当前位置:网站首页>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 */
边栏推荐
- XML基础知识概念
- [detailed explanation of AUTOSAR 14 startup process]
- 【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
- 公司破产后,黑石们来了
- 2022年阿里Android高级面试题分享,2022阿里手淘Android面试题目
- Tianyi cloud understands enterprise level data security in this way
- MYSQL中 find_in_set() 函数用法详解
- A2L file parsing based on CAN bus (3)
- Postman核心功能解析 —— 参数化和测试报告
- China law network joins hands to observe the cloud, and the online system is a full link observable platform
猜你喜欢
Use of websocket tool
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
公司破产后,黑石们来了
Why can't Bi software do correlation analysis? Take you to analyze
A cloud opens a new future of smart transportation
AI open2022 | overview of recommendation systems based on heterogeneous information networks: concepts, methods, applications and resources
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(下)
Blue sky drawing bed Apple quick instructions
Oracle Chinese sorting Oracle Chinese field sorting
随机推荐
2022年阿里Android高级面试题分享,2022阿里手淘Android面试题目
Talking about fake demand from takeout order
Deep copy and shallow copy [interview question 3]
Is it complicated to open an account? Is online account opening safe?
RPC protocol details
[HCIA cloud] [1] definition of cloud computing, what is cloud computing, architecture and technical description of cloud computing, Huawei cloud computing products, and description of Huawei memory DD
ROS installation error sudo: rosdep: command not found
Go deep into the underlying C source code and explain the core design principles of redis
A cloud opens a new future of smart transportation
R语言使用lubridate包处理日期和时间数据实战
2022 latest intermediate and advanced Android interview questions, [principle + practice + Video + source code]
华律网牵手观测云,上线系统全链路可观测平台
XML basic knowledge concept
MySQL优化六个点的总结
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
一朵云开启智慧交通新未来
The monthly list of Tencent cloud developer community videos was released in May 2022
机器学习基础(三)——KNN/朴素贝叶斯/交叉验证/网格搜索
What are the cache interfaces of nailing open platform applet API?
AI Open2022|基于异质信息网络的推荐系统综述:概念,方法,应用与资源