SIKI学习——C#初级教程03

本文详细介绍了C#中的流程控制,包括分支结构的if、if-else、三元运算符和switch语句,以及循环结构的while、do-while和for循环。还讨论了break、continue的使用,以及goto语句在跳出循环和函数中的应用。此外,提到了字符串中字符的访问方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1-流程控制

  1. 为什么使用流程控制
    根据程序中当时的环境执行不同的代码,需要重复执行某段代码。
    这两种方法就是需要用到流程控制中的分支和循环。
    分支:有条件的执行代码
    循环:重复执行相同的代码
  2. 布尔运算
int score = 90;
bool res = score >= 50;
  1. 布尔运算符
    在这里插入图片描述
  2. 布尔运算符-处理布尔值
    在这里插入图片描述
  3. 条件布尔运算符
    在这里插入图片描述
  4. 布尔赋值运算符
    在这里插入图片描述
  5. 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);
  1. 分支-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");
            }
  1. 分支-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>
    }

  2. 三元运算符
    语法
    < test> ? < resultIfTrue> : < resultIfFalse>

			int myInteger = 100;
            string str = (myInteger < 10) ? "LessThan10" : "GreaterThanorequalto10";
            Console.WriteLine(str);
  1. 分支-if语句的其他结构
if (){
}else if(){
}else if(){
}else{
}
else if可以有0或者多个
else 只能有0或者1个
  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;
            }
  1. while循环
    语法结构
    while(< test>){
    < code to be looped>
    }
			while (true)//死循环,一直执行循环体,根本停不下来
            {
                Console.WriteLine("111111");
            }

这个可以摆脱死循环

			int index = 1;
            while (index<=9)
            {
                Console.WriteLine(index);
                index++;
            }
  1. 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 (; ; )
            {
                //初始化条件和循环的判断条件都不写的话就是一个死循环
            }
  1. 循环的终止 break
    break(终止当前循环)
    使用break立即跳出循环
			int index = 1;
            while (true)
            {
                Console.WriteLine(index);
                if(index==9)
                {
                    break;//跳出最近的循环结构,执行下一行代码
                }
                index++;
            }
  1. 循环的中断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;

每个字符相当于一个整数,字符存取的时候,使用这个整数存取的,然后我们的字符可以当成一个整数来用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值