当前位置:网站首页>C#流程控制语句
C#流程控制语句
2022-07-28 19:40:00 【今昭双子】
C#流程控制语句分别有判断语句(条件语句)、循环语句、循环控制语句
一、判断语句(条件语句)
- if....else语句
- switch....case语句
- 三元运算符
if条件语句
C#支持数学中逻辑条件:
小于(<):x<y
大于(>):x>y
等于(=):x=y
小于或等于(<=):x<=y
大于或等于(>=):x>=y
不等于(!=):x!=y
可以使用这些条件对不同的决定执行不同的操作
C#具有以下条件语句:
如果指定的条件为true,则使用if指定要执行的代码块
如果相同的条件为false,则使用else指定要执行的代码块
如果第一个条件为false,则使用else if 指定要判断的另一个条件
使用switch指定许多要执行的可选代码块
- if条件语法
如果条件为true,使用if语句指定要执行的C#代码块
语法:
if (condition)
{
//如果条件为true,则执行代码块
}
例子:
下面例子中,判断两个值10是否小于20,如果条件为true,则控制台打印出“10小于20”文本
if (10 < 20)
{
Console.WriteLine("10小于20");
}
还可以判断变量
int x = 15;
int y = 20;
if (x < y)
{
Console.WriteLine("x<y");
}
2.else语句语法
如果条件为false,则使用else语句指定执行的代码块
语法:
if (condition)
{
//如果条件为true,则执行代码块
}
else
{
// 如果条件为false,则执行代码块
}
例子:
下面例子中,判断num是否小于15,如果是true,则控制台输出“num小于15”;如果是false,则控制台输出“num大于15”。结果控制台输出的是“num大于15”
int num = 20;
if(num < 15)
{
Console.WriteLine("num小于15");
}
else
{
Console.WriteLine("num大于15");
}
//输出"num大于15"
3.else if语句语法
如果第一条件为false,则使用else if语句指定下一个条件
语法:
if (condition1)
{
//如果condition1为true时要执行代码块
}
else if(condition2)
{
// 如果condition1为false,condition2为true,则执行代码块级
}
else
{
// condition和condition2为false时要执行代码块
}
例子:
int num = 20;
if (num < 15)
{
Console.WriteLine("num小于15");
}
else if (num > 25)
{
Console.WriteLine("num大于25");
}
else
{
Console.WriteLine("num等于20");
}
//输出"num等于20"
4.嵌套的if else语句
if else语句是可以嵌套的,也就是说你可以在另一个if或者else if语句中使用if或者else if语句
语法:
if (condition1)
{ //如果condition1布尔表达式的值为true执行代码
if (condition2)
{
//如果condition2布尔表达式的值为true执行代码
}
}
例子:
int x = 30;
int y = 10;
if (x == 30)
{
if(y == 10)
{
Console.WriteLine("x = 30 and y = 10");
}
//输出"x = 30 and y = 10"
if (y < 10)
{
Console.WriteLine("1");
}else if (y < 20)
{
Console.WriteLine("2");
}
else
{
Console.WriteLine("3");
}
//输出"2"
}
5.if else语句简写(三元运算符)
if else条件语句也可以使用一个简写形式,它被称为三元运算符,因为它由三个操作数组成,它可以用来用一行代替多行代码,它通常用于替换简单的if else语句
语法:
variable = (condition) ? expressionTrue : expressionFalse;
例子:
int num = 20;
if(num > 25)
{
Console.WriteLine("早上好");
}
else
{
Console.WriteLine("中午好");
}
简写:
int num = 20;
string result = (num > 25) ? "早上好" : "中午好";
Console.WriteLine(result);
6.switch
switch中的表达式是一个常量表达式,必须是一个整型或枚举类型,且值不可以相同
语法:
switch (expresstion)
{
case a:
//代码块
break;
case b:
//代码块
break;
default:
//代码块
}
switch case语句有如下规则:
switch语句中的expression必须是一个整型或枚举类型,或者是一个class类型,其中有一个单一的转换函数将其转换为整型或枚举类型。
在一个switch中可以有任意数量的case语句,每个case后跟一个要比较的值和一个冒号,case的constant-expression 必须与switch中的变量具有相同的数据类型,且必须是一个常量,当被测试的变量等于case中的常量时,case后跟的语句将被执行,直到遇到break语句为止,当遇到break语句时,switch终止,控制流将转到switch语句后的下一行。
例子:
int num = 15;
switch (num)
{
case 5:
Console.WriteLine("5");
break;
case 15:
Console.WriteLine("15");
break;
default:
Console.WriteLine("都不成立");
break;
}
//输出"num等于15"
二、循环语句
只要达到指定条件,循环就可以执行代码块
循环很方便,因为它们可以节省时间,减少错误并且使代码更具可读性
循环语句中有while循环、for循环
- while循环
只要指定条件为true,while循环就会循环遍历一段代码
例子:
int num = 0;
while(num < 6)
{
num++;
Console.WriteLine(num);
}
注意:一定要改变while条件中使用的变量,否则循环不会结束
2.do while循环语句
do while循环是while循环的变体。在检查条件是否为true之前,此循环将执行一次代码块,然后只要条件为true,它将重复该循环
例子:
int num = 0;
do
{
num++;
Console.WriteLine(num);
} while (num < 6);
3.for循环
当确切地知道要遍历代码块的次数时,使用for循环更方便而不是while循环
语法:
for(statement1; statement2; statement3)
{
//要执行的代码块
}
statement1在执行代码块之前执行(一次)
statement2定义了执行代码块的条件
在执行代码块后(每次)都会执行statement3
例子:
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
4.foreach循环
语法:
foreach(type variableName:arrayName){
//要执行的代码块
}
例子:
string[] arr = { "苹果","葡萄","蓝莓","西瓜"};
foreach(var item in arr)
{
Console.WriteLine(item);
}
三、循环控制语句
1.break关键字
break的作用是跳出当前循环代码块(for、while、do while)或switch代码块。在循环代码块中的作用就是跳出当前正在循环的循环体。在switch代码块中的作用是中断和下一个case条件的比较
例子:
下面例子中当i等于5时,跳出循环
for(int i = 0; i < 10; i++)
{
if(i == 5)
{
break;
}
Console.WriteLine(i);
}
2.continue关键字
continue用于结束循环体中其后语句的执行,并跳回循环程序块的开头执行循环
例子:
下面例子中当i等于5时,跳过本次循环
for(int i = 0; i < 10; i++)
{
if(i == 5)
{
continue;
}
Console.WriteLine(i);
}
边栏推荐
- Dom4J的Bug
- Color finder actual combat (QT including source code)
- Confession of a graduate student: why am I addicted to opengauss community?
- After Europe, it entered Japan and South Korea again, and the globalization of Pico consumer VR accelerated
- There have been two safety accidents in a month after listing. Is L9 ideal?
- [Zhou Zhou has a prize] cloud native programming challenge "edge container" track invites you to fight!
- 基于Xilinx的时序分析与约束
- ABB电磁流量计维修信号变送器维修41F/E4技术参数
- 【题目】两数相加
- Bus, protocol, specification, interface, data acquisition and control system in industrial communication field
猜你喜欢
![[Zhou Zhou has a prize] cloud native programming challenge](/img/0d/e26e37cddf3cf01b5e9dcaf7211106.png)
[Zhou Zhou has a prize] cloud native programming challenge "edge container" track invites you to fight!

两款吾爱破解优秀软件,批量查找文本,图像视频画质增强

The greatest romance of programmers~

Redis cache avalanche, cache penetration, cache breakdown

Young freshmen yearn for more open source | here comes the escape guide from open source to employment!

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

The development of smart home industry pays close attention to edge computing and applet container technology

Guo Mingxuan: meta contraction is conducive to the development of VR competitors, and apple XR headshow will change the industry rules

速卖通测评自养号,国外环境如何搭建?需要多少成本?

Applet container technology improves mobile R & D efficiency by 500%
随机推荐
怎么理解数据网格(Data Mesh)
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
Go并发编程基础
The development of smart home industry pays close attention to edge computing and applet container technology
protobuf 中基础数据类型的读写
Kubeadm搭建kubernetes集群
Moco V1: the visual field can also be self supervised
The 35 required questions in MySQL interview are illustrated, which is too easy to understand
Nacos 原理
智能家居行业发展,密切关注边缘计算和小程序容器技术
unity-shader-1
MySQL sorts out the review content -- with mind map
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
Ctfshow question making web module web11~web14
Unity knowledge points summary (1)
quii cordova-plugin-telerik-imagepicker插件多图上传乱序
How does lazada store make up orders efficiently? (detailed technical explanation of evaluation self-supporting number)
How to modify the ID of NetApp expansion enclosure disk shelf
Eureka registers with each other, only showing each other or only showing problems in one
Cause analysis of restart of EMC cx4-120 SPB controller