1-流程控制
- 为什么使用流程控制
根据程序中当时的环境执行不同的代码,需要重复执行某段代码。
这两种方法就是需要用到流程控制中的分支和循环。
分支:有条件的执行代码
循环:重复执行相同的代码 - 布尔运算
int score = 90;
bool res = score >= 50;
- 布尔运算符
- 布尔运算符-处理布尔值
- 条件布尔运算符
- 布尔赋值运算符
- goto语句
在程序中我们可以给代码加上标签,这样就可以使用goto
语句直接调到标签的地方去执行
goto语句的语法
goto < labelName>;
标签定义
< labelName>:
实例:
int myInteger = 5;
goto myLabel;
myInteger ++;
myLabel:
Console.WriteLine(myInteger);
int myInteger = 5;
goto mylabel;//goto语句用来控制程序跳转到某个标签的位置
myInteger++;//不执行这句了
mylabel: Console.WriteLine(myInteger);
- 分支-if语句——使用if语句可以有条件的执行某段代码
if的语法
if(< test>)
<code executed if < test> is true>
先执行< test>,如果结果是true就执行
string str = Console.ReadLine();
int score = Convert.ToInt32(str);
if (score>50)
{
Console.WriteLine("您输入的分数大于50");
}
if (score <= 50)
{
Console.WriteLine("您输入的分数小于等于50");
}
-
分支-if-else语句
if else语法
if(< test>)
<code executed if < test> is true>
else
<code executed if < test> is false>
如果if和else要执行的代码有多行,可以加上{}组成一个块
if(< test>){
<code executed if< test> is true>
}else{
<code executed if < test> is false>
} -
三元运算符
语法
< test> ? < resultIfTrue> : < resultIfFalse>
int myInteger = 100;
string str = (myInteger < 10) ? "LessThan10" : "GreaterThanorequalto10";
Console.WriteLine(str);
- 分支-if语句的其他结构
if (){
}else if(){
}else if(){
}else{
}
else if可以有0或者多个
else 只能有0或者1个
- switch语句 - 基本语法
switch语句类似于if语句,switch可以用来将测试变量跟多个值
进行比较。switch的语法结构如下:
switch (<testvar>){
case <comparisonVal1>:
<code to execute if <testvar> == <comparisionVal1>>
break;
case <omparisonVal2>:
<code to execute if <testvar> == <comparisionVal2>>;
break;
...
case <comparisionN>:
<code to execute if <testvar>==<comparisionValN>>;
break;
default:
<code to execute if <testvar>!=<comparisionVals>>
break;
}
<testvar> 这里不管直接放一个字面值还是变量,它的类型是数值类型跟char类型
例子:
int state = 5;
switch (state)
{
case 0:
Console.WriteLine("当前是开始界面");
break;
case 1:
Console.WriteLine("当前是战斗中");
break;
case 2:
Console.WriteLine("游戏暂停");
break;
case 3:
Console.WriteLine("游戏胜利");
break;
case 4:
case 5:
Console.WriteLine("游戏失败");
break;
default:
Console.WriteLine("当前state超出了游戏状态的取值范围");
break;
}
- while循环
语法结构
while(< test>){
< code to be looped>
}
while (true)//死循环,一直执行循环体,根本停不下来
{
Console.WriteLine("111111");
}
这个可以摆脱死循环
int index = 1;
while (index<=9)
{
Console.WriteLine(index);
index++;
}
- do循环
语法结构
do{
< code to be looped>;
}while(< test>);
< test>返回的是一个bool值(循环的条件判断)
do
{
Console.WriteLine("这个是循环体(死循环)");
}
while (true);
int index = 1;
do
{
Console.WriteLine(index);
index++;
}
while (index <= 9);
do while 循环会首先执行一次循环体,然后进行条件判断,循环体的执行次数>=1,while循环会先进行条件判断,然后根据判断的结果去判定是否去执行循环体,循环体的次数>=0。
15. for循环
语法结构
for(<initialization;< condition>;< operation>>){
< code to loop>
}
< initialization>是初始化,这里可以定义一个变量,也可以给变量赋值
< condition>是判断是否执行循环的条件
< operation>每次执行完循环都会执行operation代码
for (int i = 0; i <= 9; i++)
{
Console.WriteLine(i);
}
int i = 0;
for (; i <= 9; )
{
Console.WriteLine(i);
i++;
}
for (; ; )
{
//初始化条件和循环的判断条件都不写的话就是一个死循环
}
- 循环的终止 break
break(终止当前循环)
使用break立即跳出循环
int index = 1;
while (true)
{
Console.WriteLine(index);
if(index==9)
{
break;//跳出最近的循环结构,执行下一行代码
}
index++;
}
- 循环的中断continue
continue(终止当前循环继续下一个循环)
使用continue,只会终止当次循环,继续运行下次循环
int index = 1;
while (true)
{
index++;
if (index==5)
{
continue;
}
if (index==10)
{
break;
}
Console.WriteLine(index);
}
当index==5时,使用了continue关键字,那么continue后面的代码不会去执行了,直接会进行循环的条件判断,根据判断结果判定是否执行下一次循环。
循环的中断
goto语句可以直接跳到某一个位置
return跳出循环(跳出函数)
18. 补充内容
string str = "eewwm";
char c = str[0];
我们可以通过字符串[index]这种形式来访问字符串中指定的某个字符,默认字符索引index是从0开始的。
char C = 'B';
int num = C;
每个字符相当于一个整数,字符存取的时候,使用这个整数存取的,然后我们的字符可以当成一个整数来用。