当前位置:网站首页>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);
}
边栏推荐
- How to build a foreign environment for the self-supporting number of express evaluation? How much does it cost?
- 如何度量软件架构
- Uncaught Error:Invalid geoJson format Cannot read property ‘length‘ of undefind
- Ctfshow question making web module web11~web14
- Maxwell 一款简单易上手的实时抓取Mysql数据的软件
- The development of smart home industry pays close attention to edge computing and applet container technology
- BUUCTF做题Upload-Labs记录pass-01~pass-10
- 九鑫智能正式加入openGauss社区
- 工业通讯领域的总线、协议、规范、接口、数据采集与控制系统
- Database -- use of explain
猜你喜欢

基于Xilinx的时序分析与约束

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

Moco V2: further upgrade of Moco series

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

Ctfshow network lost track record (1)

Kubeadm搭建kubernetes集群

DELTA热金属检测器维修V5G-JC-R1激光测量传感器/检测仪原理分析
![[tidb] importing TXT documents into the database is really efficient](/img/2a/d33849987a75c4a0d52d8f0ab767ca.png)
[tidb] importing TXT documents into the database is really efficient

Maxwell 一款简单易上手的实时抓取Mysql数据的软件

35 道 MySQL 面试必问题图解,这样也太好理解了吧
随机推荐
Timing analysis and constraints based on Xilinx
Introduction to blue team: efficiency tools
Backup and recovery of SQL Server database
ctfshow 网络迷踪做题记录(1)
[tidb] importing TXT documents into the database is really efficient
Coding with these 16 naming rules can save you more than half of your comments!
Tested interviewed Zuckerberg: reveal more details of four VR prototypes
Link with bracket sequence I (state based multidimensional DP)
Go concurrent programming basics
一名在读研究生的自白:我为什么会沉迷于openGauss 社区?
智能家居行业发展,密切关注边缘计算和小程序容器技术
Link with Bracket Sequence I(状态基多维dp)
八、QOS队列调度与报文丢弃
Maintenance of delta hot metal detector principle analysis of v5g-jc-r1 laser measurement sensor / detector
学习Typescript(二)
基于Xilinx的时序分析与约束
Zcmu--5066: dark corridor
Why on earth is it not recommended to use select *?
Cloud security core technology
A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year