C#流程控制详解

目录

流程控制语句分类

1、if语句

2、switch

3、三位运算符

4、迭代语句之while语句

4.1 迭代语句概述

4.2 while语句

5、迭代语句之do……while

6、迭代语句之for循环语句

7、迭代语句之foreach

8、跳转语句之break语句

9、continue语句

10、跳转语句之return

11、跳转语句之goto

12、任务实施

流程控制语句分类

分支语句: if语句和switch语句

迭代语句

跳转语句

1、if语句

if (判断条件表达式){ 表达式结果为true时执行}else{表达式结果为false时执行}

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace if语句 { class Program { static void Main(string[] args) { //判断a变量与10的关系 Console.WriteLine("请输入你要比较的第一个数字"); int a=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入你要比较的第而个数字"); //int.parse 用于将屏幕输入的语句转换为整型 int b = int.Parse(Console.ReadLine()); if (a < b) { Console.WriteLine("您输入的第一个数字{0}小于第二个数字{1}", a,b); } else if (a == b) { Console.WriteLine("您输入的第一个数字{0}等于第二个数字{1}", a,b); } else { Console.WriteLine("您输入的第一个数字{0}大于第二个数字{1}", a,b); } Console.ReadKey(); } } } 2、switch

输入1显示为星期一,依次类推

swithc(条件表达式){
case 常量表达式:条件语句;
case 常量表达式:条件语句;
case 常量表达式:条件语句;
default:条件表达式
}

控件无法从最终用例标签(XX)脱离开关——程序无法判定为结束,所以必须加一个break;

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace switch控制语句 { class Program { static void Main(string[] args) { // 输入一显示星期一,一次类推 Console.WriteLine("请输入1-7的数字"); int week = int.Parse(Console.ReadLine()); switch (week) { case 1: Console.WriteLine("星期一"); break; //结束当前代码体 case 2: Console.WriteLine("星期二"); break; case 3: Console.WriteLine("星期三"); break; case 4: Console.WriteLine("星期四"); break; case 5: Console.WriteLine("星期五"); break; case 6: Console.WriteLine("星期六"); break; case 7: Console.WriteLine("星期日"); break; default: Console.WriteLine("您输入的数据错误"); break; //超出规定值设置相应提示 } Console.ReadKey(); //判断2020年每个月的天数, 1,3,5,7,8,10,12为31天,4,6,9,11位30天,二月29天 Console.WriteLine("请输月份数"); int month = int.Parse(Console.ReadLine()); switch (month) { case 2: Console.WriteLine("您输入的{0}月份有28天",month); break; case 4: case 6: case 9: case 11: Console.WriteLine("您输入的{0}月份有30天",month); break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: Console.WriteLine("您输入的{0}月份有31天", month); break; default: Console.WriteLine("您输入的{0}月份错误", month); break; } Console.ReadKey(); } } } 3、三位运算符

条件判断表达式?成立是执行的语句:不成立时执行的语句
三元运算符适用条件:只使用与判断具有两个结果的情况

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 三位运算符 { class Program { static void Main(string[] args) { // 判断输入述职与10的关系(<10 提示小于10, >=10提示大于等于10) Console.WriteLine("请输入您要比较的数据"); int number = int.Parse(Console.ReadLine()); //Console.WriteLine(number < 10 ? Console.WriteLine("小于10") : Console.WriteLine("大于等于10") ); Console.WriteLine(number < 10 ? "小于10" : "大于等于10"); Console.ReadKey(); } } }

4、迭代语句之while语句 4.1 迭代语句概述

迭代语句时程序中重复的执行,直到满足指定以条件才停止的一段代码。当用户想重复执行某些语句时,可依据当前不同的任务,

选择不同的循环依据使用,分别是:

while语句

do……while语句

for语句

foreach语句

4.2 while语句

while(条件表达式){
代码语句
}
while语句当条件满足时才执行

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace while语句 { class Program { static void Main(string[] args) { //输出1-50的数字到屏幕上 int a = 1; while (a<=50){ Console.WriteLine(a); a++; } Console.ReadKey(); } } } 5、迭代语句之do……while

do{
循环体语句
}while();
do……while语句至少执行一次,即使条件不成立也会执行一次

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace do__while { class Program { static void Main(string[] args) { //输出1-50的数字到屏幕上 int num = 0; do { num++; Console.WriteLine(num); } while (num < 50); // 计算现金存入银行多长时间才可以答案到我们的预期收益(均按一年期定期存款,到期后自动转存) // 分析题目需要的变量 :本金, 目标收益,利率 时间(年) // 一年的收益: 本金*(1+利率)*1年 double Balace = 0; double Rate = 0; int Year = 0; double TargetBalace = 0; Console.WriteLine("请输入您的本金"); Balace = double.Parse(Console.ReadLine()); Console.WriteLine("请输入您的当前利率百分比"); Rate = double.Parse(Console.ReadLine())/100; Console.WriteLine("请输入您的目标收益"); do { TargetBalace = double.Parse(Console.ReadLine()); if (TargetBalace<Balace) { Console.WriteLine("恭喜您现在已经拥有了{0}元,请输入一个更大的目标",TargetBalace); } } while (TargetBalace<Balace); do { Balace *= (Rate + 1); Year++; } while (Balace < TargetBalace); Console.WriteLine("您将在{0}年内,获得{1}元的收益",Year,Balace); Console.ReadKey(); } } } 6、迭代语句之for循环语句

for循环可以循环次数的限定,并维护自己的计时器;
有时候我们会省略初始条件,判断条件,循环条件,但两个分号不能省略

for(初始条件;判断条件;循环条件){
循环语句
}

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace for循环语句 { class Program { static void Main(string[] args) { //求输入数据的阶乘 // 1!=1 2!=2x1; 3!=3x2x1 Console.WriteLine("请输入你要计算的阶乘数"); for (;;) { int num = int.Parse(Console.ReadLine()); int result = 1; for (int i=num; i!=0; i--) { result *= i; }; Console.WriteLine("{0}的阶乘结果是{1}", num, result); }; //Console.ReadKey(); } } }

for循环嵌套(九九乘法表)
循环嵌套就是一个循环中嵌套着另一个循环
使用for循环时,一般在for循环语句进行声明循环计数次的变量

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace for循环语句 { class Program { static void Main(string[] args) { //九九乘法表 Console.WriteLine("==================九九乘法口诀========================="); for (int i = 1; i < 10; i++) { for (int j=1; j<= i; j++) { Console.Write("{0}X{1}={2}\t", j, i, j * i); } Console.WriteLine(); } Console.ReadKey(); } } } 7、迭代语句之foreach

foreach提供了一个for语句的捷径,而且还存进了集合类更为一致

foreach(类型;变量;in 集合){
代码体
}

string类型(字符串)可以看成是char类型(字符)的一个集合
char.IsWhiteSpace© 判断字符是不是空格
foreach每执行一内含代码,循环变量就会一次读取集合中的一个元素,向当时循环便利
此处循环变量只是一个只读型的局部变量,这个值如果被修改编译会报错

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace @foreach { class Program { static void Main(string[] args) { //将语句识别为单词,并逐行输出 //语句用string类型,字母用char Console.WriteLine("请输入一句英文语句"); string sentence = Console.ReadLine(); foreach (char word in sentence) { if (char.IsWhiteSpace(word)) { Console.WriteLine(); } else { Console.Write(word); //word='t'; //foreach语句的迭代变量不允许重新赋值 } } Console.ReadLine(); } } } 8、跳转语句之break语句

跳转语句是程序运行到某一位置时,可以跳转到程序中另一行代码的语句

break:1)switch语句中用于从case语句中跳出,结束switch分支语句。2)用于跳出迭代语句结束当前训话

continute语句

goto语句

return语句

通过迭代语句,准备输出1~500这500个数,每行输出10个数。当输出的值同时是2、3、4、5、6】7的倍数是,跳出for迭代语句。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace break语句 { class Program { static void Main(string[] args) { //通过迭代语句,准备输出1~500这500个数,每行输出10个数。当输出的值同时是2、3、4、5、6、7的倍数是,跳出for迭代语句。 Console.WriteLine("输出1~500这500个数,每行输出10个数"); for (int i=1;i<501;i++) { if (i % 2 == 0 && i % 3 == 0 && i % 4 == 0 && i % 5 == 0 && i % 6 == 0 && i % 7 == 0) { Console.WriteLine(); Console.WriteLine("2、3、4、5、6、7的最小公倍数倍数是"+i); break; } if (i % 10 == 0) { Console.WriteLine(i); } else Console.Write(i + "\t"); } Console.ReadKey(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace break语句 { class Program { static void Main(string[] args) { //通过迭代语句,准备输出1~500这500个数,每行输出10个数。当输出的值同时是2、3、4、5、6、7的倍数是,跳出for迭代语句。 Console.WriteLine("输出1~500这500个数,每行输出10个数"); for (int i=1;i<501;i++) { if (i % 2 == 0 && i % 3 == 0 && i % 4 == 0 && i % 5 == 0 && i % 6 == 0 && i % 7 == 0) break; //{ // Console.WriteLine(); // Console.WriteLine("2、3、4、5、6、7的最小公倍数倍数是"+i); // break; //} if (i % 10 == 0) { Console.WriteLine(i); } else Console.Write(i + "\t"); } Console.ReadKey(); } } } 9、continue语句

用于停止当前的迭代语句,结束本次循环,进入下一次循环(本次循环中continue后面的语句不执行)。breack是直接结束循环
只能用于迭代语句中

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace continute语句 { class Program { static void Main(string[] args) { //实现50以内的奇数输出,利用continue Console.WriteLine("请输入一个数,会自动显示小于次数的所有奇数"); int num = int.Parse(Console.ReadLine()); for (int i = 1; i < num+1; i++) { if (i % 2 == 0) continue; //满足条件时跳出此次循环,进入下一个循环;且本次循环continute后的语句不执行 Console.WriteLine(i); } Console.ReadLine(); } } } 10、跳转语句之return

return语句使用时,一般有两种格式:1)return; 2)return 表达式;
return语句只能出现在方法当中,当调佣方法时,执行到return语句时;直接跳转到main()函数

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace continute语句 { class Program { static void Main(string[] args) { //实现50以内的奇数输出,利用continue Console.WriteLine("请输入一个数,会自动显示小于次数的所有奇数"); int num = int.Parse(Console.ReadLine()); for (int i = 1; i < num+1; i++) { if (i % 2 == 0) continue; //满足条件时跳出此次循环,进入下一个循环;且本次循环continute后的语句不执行 Console.WriteLine(i); } Console.ReadLine(); } } }

使用方法实现:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace @return { class Program { static void Main(string[] args) { Console.WriteLine("请输入三个整数,按回车键确认每个数的输入"); int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int c = int.Parse(Console.ReadLine()); //double averageresult = (a + b + c) / 3; double averageresult = average(a,b,c); Console.WriteLine("您输入的三个数{0}、{1}、{2}的平均数是{3}",a,b,c, averageresult); Console.ReadKey(); } static double average(int a, int b, int c) { return (a + b + c) / 3; } } } 11、跳转语句之goto

格式:goto 标标识符;
标识符标识程序位置的方法
标识方法——标识符+“:”

作用:当程序执行到goto语句时,程序会直接跳转到标识符所表示的程序位置。继续执行
goto的使用会使代码的易读性下降,在编写程序的时候尽量少用goto语句

任务:利用goto语句实现选择题:

5!=?
1、5!=5
2、5!=10
3、5!=20
4、5!=60

如果选择真确,提示:恭喜你,答对了!
如果选择错误,提示:很遗憾,你答错了
如果选择的选项不是1、2、3、4,提示:你所选的选项不存在

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace goto语句 { class Program { static void Main(string[] args) { int a = 0; Console.WriteLine("请选择5的阶乘正确答案,输入选项编号回车键确认"); Console.WriteLine("1. 5!=5\n2. 5!=10\n3. 5!=20\n4. 5!=60\n"); error: { a++; //第一次执行时 a=1;因此不执行,当goto跳转到此语句时,再次自加1,a=2此时执行下面语句 if (a > 1) Console.WriteLine("很遗憾,您打错了,请重新输入答案"); // 加入a判断条件原因是,避免在第一次执行是输出此提示 } input: int result = int.Parse(Console.ReadLine()); switch (result) { case 1: case 2: case 3: goto error; case 4: goto right; default: Console.WriteLine("您的选项{0}不存在,请重新输入",result); goto input; } right: { Console.WriteLine("恭喜你答对了!"); } Console.ReadKey(); } } } 12、任务实施

接受a\b\c三个整数,然后输出三个数中居中的那个数,并输出其阶乘

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 任务实施 { class Program { static void Main(string[] args) { Console.WriteLine("请输入三个整数"); int a = Convert.ToInt32(Console.ReadLine()); int b = Convert.ToInt32(Console.ReadLine()); int c = Convert.ToInt32(Console.ReadLine()); //判断中间变量 ///如果a是中间值,那么有两种情况,b是最大值或b是最小值 int temp = 0; int jc = 1; if ((a>=b && a<=c) || (a>=c && a<=b)) { Console.WriteLine(a + "是中间值"); temp = a; Console.WriteLine("错误"); } if (b >= a && b <= c || b >= c && b <= a) { Console.WriteLine(b + "是中间值"); temp = b; } if (c >= a && c <= b || c >= b && c <= a) { Console.WriteLine(c + "是中间值"); temp = c; } for (int i = 1; i < b+1; i++) { jc *= i; } Console.WriteLine("中间数{0}阶乘结果是{1}",temp,jc); Console.ReadKey(); } } }

到此这篇关于C#流程控制详解的文章就介绍到这了,更多相关C#流程控制内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!

推荐阅读

    foreach的用法c语言和c#

    foreach的用法c语言和c#,数组,遍历,本文目录foreach的用法c语言和c#详细讲解foreach循环的用法C#中的foreach 怎么用啊foreach用法C#中的f

    params 是什么意思,c#里

    params 是什么意思,c#里,参数,数组,本文目录params 是什么意思,c#里params.add跟params.put有区别吗C#中ref,out和params有什么区别发送请

    C# 基于StackExchange.Redis.dll利用Redis实现分布式Session

    C# 基于StackExchange.Redis.dll利用Redis实现分布式Session,令牌,客户端,最近在研发一款O2O产品,考虑到分布式架构的需要,以前那一套.NET的

    c#中 (ToolStrip

    c#中 (ToolStrip,窗体,控件,本文目录c#中 (ToolStrip)控件是做什么用的winform窗体问题 toolstrip怎么重用,每个窗体都要用到同样的工具栏窗体

    C#取得DataTable最大值、最小值

    C#取得DataTable最大值、最小值,最大值,最小值,C#取得DataTable最大值、最小值int max=int.Parse((dtItemsAll.Compute("Max(CPITEMS_SOR

    C#如何打印|cctv5体育节目表

    C#如何打印|cctv5体育节目表,,C#如何打印最省事的办法,引用word对象,再把word弹出来用word打印,比较适合你这样刚学的,还不容易出错。cctv5体

    C#中的listview控件怎么用

    C#中的listview控件怎么用,控件,第一个,C#中的listview控件怎么用比如你在窗体上放了一个ListView,教你一些简单的操作。listView1.Columns