C#初级教程-C#常用运算符

布尔运算
在这里插入图片描述
布尔运算符-处理布尔值
在这里插入图片描述
条件布尔运算符
在这里插入图片描述
if else语法
if()
<code executed if is true>
else
<code executed if is false>

如果if和else要执行的代码有多行,可以加上{}组成一个块

if(<test>){
	<code executed if <test> is true>
}else{
	<code executed if <test> is false>
}

else if可以有0或者多个
else 只能有0或者1个
if (){
}else if(){
}else if(){
}else{
}

三元运算符
语法
? :

示例
string resStr = (myInteger<10) ? “Less than 10”:“Greater than or equal to 10”
switch语句 - 基本语法
switch语句类似于if语句,switch可以用来将测试变量跟多个值
进行比较。switch的语法结构如下:
switch (){
case :
<code to execute if == >
break;
case :
<code to execute if == >;
break;

case :
<code to execute if ==>;
break;
default:
<code to execute if !=>
break;
}
这里不管直接放一个字面值还是变量,它的类型是数值类型跟char类型
do循环
语法结构

do{
	<code to be looped>;
}while(<test>);

返回的是一个bool值(循环的条件判断)

使用循环输出1-9
while循环
语法结构
while(){

}

使用while输出 1-9
for循环
语法结构
for(<initialization;;>){

}

是初始化,这里可以定义一个变量,也可以给变量赋值
是判断是否执行循环的条件
每次执行完循环都会执行operation代码

循环的中断 break(终止当前循环)
使用break立即跳出循环
使用continue,只会终止当次循环,继续运行下次循环
return 跳出循环

猜你喜欢

转载自blog.csdn.net/euphorias/article/details/104478482
今日推荐