当前位置:网站首页>C process control statement
C process control statement
2022-07-28 21:25:00 【Jinzhao Gemini】
C# The process control statements are Judgment statement ( Conditional statements )、 Loop statement 、 Loop control statement
One 、 Judgment statement ( Conditional statements )
- if....else sentence
- switch....case sentence
- Ternary operator
if Conditional statements
C# Support logical conditions in Mathematics :
Less than (<):x<y
Greater than (>):x>y
be equal to (=):x=y
Less than or equal to (<=):x<=y
Greater than or equal to (>=):x>=y
It's not equal to (!=):x!=y
You can use these conditions to perform different actions on different decisions
C# Have the following conditional statements :
If the specified condition is true, Then use if Specify the code block to execute
If the same condition is false, Then use else Specify the code block to execute
If the first condition is false, Then use else if Specify another condition to judge
Use switch Specify a number of optional code blocks to execute
- if Conditional grammar
If the condition is true, Use if Statement specifies the C# Code block
grammar :
if (condition)
{
// If the condition is true, Then execute the code block
}
Example :
In the following example , Judge two values 10 Is less than 20, If the condition is true, Then the console prints out “10 Less than 20” Text
if (10 < 20)
{
Console.WriteLine("10 Less than 20");
}
You can also judge variables
int x = 15;
int y = 20;
if (x < y)
{
Console.WriteLine("x<y");
}
2.else Sentence syntax
If the condition is false, Then use else Statement specifies the code block to execute
grammar :
if (condition)
{
// If the condition is true, Then execute the code block
}
else
{
// If the condition is false, Then execute the code block
}
Example :
In the following example , Judge num Is less than 15, If it is true, Then the console outputs “num Less than 15”; If it is false, Then the console outputs “num Greater than 15”. As a result, the console outputs “num Greater than 15”
int num = 20;
if(num < 15)
{
Console.WriteLine("num Less than 15");
}
else
{
Console.WriteLine("num Greater than 15");
}
// Output "num Greater than 15"
3.else if Sentence syntax
If the first condition is false, Then use else if Statement specifies the next condition
grammar :
if (condition1)
{
// If condition1 by true Execute code block
}
else if(condition2)
{
// If condition1 by false,condition2 by true, Then execute code block level
}
else
{
// condition and condition2 by false Execute code block
}
Example :
int num = 20;
if (num < 15)
{
Console.WriteLine("num Less than 15");
}
else if (num > 25)
{
Console.WriteLine("num Greater than 25");
}
else
{
Console.WriteLine("num be equal to 20");
}
// Output "num be equal to 20"
4. Nested if else sentence
if else Statements can be nested , That means you can be in another if perhaps else if Use in statement if perhaps else if sentence
grammar :
if (condition1)
{ // If condition1 The value of the Boolean expression is true Execute code
if (condition2)
{
// If condition2 The value of the Boolean expression is true Execute code
}
}
Example :
int x = 30;
int y = 10;
if (x == 30)
{
if(y == 10)
{
Console.WriteLine("x = 30 and y = 10");
}
// Output "x = 30 and y = 10"
if (y < 10)
{
Console.WriteLine("1");
}else if (y < 20)
{
Console.WriteLine("2");
}
else
{
Console.WriteLine("3");
}
// Output "2"
}
5.if else Statement abbreviation ( Ternary operator )
if else Conditional statements can also use a shortened form , It's called the ternary operator , Because it consists of three operands , It can be used to replace multiple lines of code with one line , It is usually used to replace simple if else sentence
grammar :
variable = (condition) ? expressionTrue : expressionFalse;
Example :
int num = 20;
if(num > 25)
{
Console.WriteLine(" Good morning ");
}
else
{
Console.WriteLine(" Good noon ");
}
Abbreviation :
int num = 20;
string result = (num > 25) ? " Good morning " : " Good noon ";
Console.WriteLine(result);
6.switch
switch The expression in is a constant expression , Must be an integer or enumeration type , And the value cannot be the same
grammar :
switch (expresstion)
{
case a:
// Code block
break;
case b:
// Code block
break;
default:
// Code block
}
switch case Sentences have the following rules :
switch Statement expression Must be an integer or enumeration type , Or a class type , There is a single conversion function that converts it to an integer or enumeration type .
In a switch There can be any number of case sentence , Every case Followed by a value to compare and a colon ,case Of constant-expression Must be with switch Variables in have the same data type , And must be a constant , When the variable being tested equals case When the constant in ,case The following statement will be executed , Until I met break The statement so far , When you meet break When the sentence is ,switch End , The control flow will go to switch The next line after the statement .
Example :
int num = 15;
switch (num)
{
case 5:
Console.WriteLine("5");
break;
case 15:
Console.WriteLine("15");
break;
default:
Console.WriteLine(" It doesn't work ");
break;
}
// Output "num be equal to 15"
Two 、 Loop statement
As long as the specified conditions are met , Loop can execute code blocks
Circulation is convenient , Because they can save time , Reduce errors and make code more readable
There is while loop 、for loop
- while loop
As long as the specified condition is true,while The loop will loop through a piece of code
Example :
int num = 0;
while(num < 6)
{
num++;
Console.WriteLine(num);
}
Be careful : We must change while Variables used in conditions , Otherwise the cycle will not end
2.do while Loop statement
do while Cycle is while A variant of the cycle . Check whether the condition is true Before , This loop will execute a block of code , Then as long as the condition is true, It will repeat the cycle
Example :
int num = 0;
do
{
num++;
Console.WriteLine(num);
} while (num < 6);
3.for loop
When you know exactly how many times you want to traverse the code block , Use for Circulation is more convenient than while loop
grammar :
for(statement1; statement2; statement3)
{
// Block of code to execute
}
statement1 Execute before executing the code block ( once )
statement2 Defines the conditions for executing code blocks
After executing the code block ( Every time ) It will be carried out statement3
Example :
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
4.foreach loop
grammar :
foreach(type variableName:arrayName){
// Block of code to execute
}
Example :
string[] arr = { " Apple "," grapes "," blueberries "," watermelon "};
foreach(var item in arr)
{
Console.WriteLine(item);
}
3、 ... and 、 Loop control statement
1.break keyword
break The function of is to jump out of the current circular code block (for、while、do while) or switch Code block . The function in the loop code block is to jump out of the current loop body . stay switch The function in the code block is to interrupt and next case Comparison of conditions
Example :
In the following example, when i be equal to 5 when , Out of the loop
for(int i = 0; i < 10; i++)
{
if(i == 5)
{
break;
}
Console.WriteLine(i);
}
2.continue keyword
continue Used to end the execution of subsequent statements in the loop body , And jump back to the beginning of the loop block to execute the loop
Example :
In the following example, when i be equal to 5 when , Skip this cycle
for(int i = 0; i < 10; i++)
{
if(i == 5)
{
continue;
}
Console.WriteLine(i);
}
边栏推荐
- 关键路径的分析
- Moco V3: visual self supervision ushers in transformer
- The framing efficiency of setpreviewcallbackwithbuffer will become lower
- (PMIC) full and half bridge drive csd95481rwj PDF specification
- CVPR 2022 | in depth study of batch normalized estimation offset in network
- 1945. 字符串转化后的各位数字之和
- 承载银行关键应用的容器云平台如何选型及建设?
- 工业通讯领域的总线、协议、规范、接口、数据采集与控制系统
- Nacos principle
- Link with bracket sequence I (state based multidimensional DP)
猜你喜欢

The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......

顶级“Redis 笔记”, 缓存雪崩 + 击穿 + 穿透 + 集群 + 分布式锁,NB 了

智能家居行业发展,密切关注边缘计算和小程序容器技术

Why on earth is it not recommended to use select *?

Nacos 原理

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!

基于Xilinx的时序分析与约束

Ijcai2022 tutorial | dialogue recommendation system

BUUCTF做题Upload-Labs记录pass-01~pass-10

关键路径的分析
随机推荐
【英雄哥七月集训】第 28天:动态规划
【云原生】什么是 CI/CD ? | 摆平交付障碍的 CI/CD
Invalid prompt object name in SQL Server
Deit: attention can also be distilled
source insight 使用快捷键
Moco V3: visual self supervision ushers in transformer
详细讲解C语言12(C语言系列)
证券企业基于容器化 PaaS 平台的 DevOps 规划建设 29 个典型问题总结
How to build a foreign environment for the self-supporting number of express evaluation? How much does it cost?
[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles
(PMIC)全、半桥驱动器CSD95481RWJ PDF 规格
在子组件中使用el-date-picker报错
Top level "redis notes", cache avalanche + breakdown + penetration + cluster + distributed lock, Nb
Mobilevit: challenge the end-to-side overlord of mobilenet
ABB electromagnetic flowmeter maintenance signal transmitter maintenance 41f/e4 technical parameters
How to measure software architecture
【Bluetooth蓝牙开发】八、BLE协议之传输层
4.2 Virtual Member Functions
【题目】两数相加
关键路径的分析