当前位置:网站首页>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);
}
边栏推荐
- Zcmu--5066: dark corridor
- 4.2 Virtual Member Functions
- Quii Cordova plugin telerik imagepicker plug-in multi image upload out of sequence
- 微服务架构下的系统集成
- 探讨:想要落地DevOps的话,只考虑好的PaaS容器平台就够了么?
- 实习日记第一周
- 广和通&高通物联网技术开放日成功举办
- DLL decompile (decompile encrypted DLL)
- Niuke turns on the camera and the picture disappears a few seconds later | the picture flashes when the camera is turned on
- Buuctf questions upload labs record pass-11~pass-20
猜你喜欢

Eureka registers with each other, only showing each other or only showing problems in one

How to modify the ID of NetApp expansion enclosure disk shelf

Unity - script lifecycle

quii cordova-plugin-telerik-imagepicker插件多图上传乱序

Redis缓存雪崩、缓存穿透、缓存击穿

ctfshow 做题 web模块 web11~web14

Ctfshow question making web module web11~web14

详细讲解C语言12(C语言系列)

Coding with these 16 naming rules can save you more than half of your comments!

The greatest romance of programmers~
随机推荐
The EMC vnx5200 fault light is on, but there is no hardware fault prompt
SQL Server 数据库之备份和恢复数据库
C language final review questions
在子组件中使用el-date-picker报错
广和通&高通物联网技术开放日成功举办
System integration under microservice architecture
Ijcai2022 tutorial | dialogue recommendation system
职场高薪 |「中高级测试」面试题
Ctfshow network lost track record (1)
The greatest romance of programmers~
程序员最大的浪漫~
Dom4J的Bug
移动端空余部位自动填充
Cloud security core technology
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
Study - 几何计算总结
到底为什么不建议使用SELECT * ?
探讨:想要落地DevOps的话,只考虑好的PaaS容器平台就够了么?
After Europe, it entered Japan and South Korea again, and the globalization of Pico consumer VR accelerated
[tidb] importing TXT documents into the database is really efficient