C# 《五》流程控制(1)
1、分支语句之 if 语句
1、流程控制语句是程序的核心部分,对任何一门编程语言来说都至关重要,是控制程序执行流向的基本语句。如果一门语言缺少了流程控制,就会缺少对程序流向的控制,就不能称之为计算机语言。
2、C#语言提供了丰富、灵活的控制流程语句,主要分分支语句、迭代语句、跳转语句三类。
分支语句为 if 语句与 switch 语句;能够根据实际情况决定逻辑路径代码。
if(判断条件表达式) { //表达式结果为 true 时执行 } else { //表达式结果为 false 时执行 }
3、对输入的数字进行判断
> 10 提示大于 10
< 10 提示小于 10
= 10 提示等于 10
4、新语句
int Parse(Console.ReadLine()); //接受用户输入的整数
程序如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5._1_分支语句_if_语句 { class Program { static void Main(string[] args) { //判断变量 a 中存放数值与 10 的关系 Console.WriteLine("请输入数值,判断它与 10 的关系:"); int a = int.Parse(Console.ReadLine()); //int.Parse 用于将屏幕输入的语句转换为整型 if(a < 10) { Console.WriteLine("a小于 10"); } if(a == 10) { Console.WriteLine("a 等于 10"); } if(a > 10) { Console.WriteLine("a 大于 10"); }/* else //如果前边的 if 语句一条也没有执行,那就执行 else 语句 //如果执行了其中一条 if 语句,那就不会执行else语句 { Console.WriteLine("无法判断"); }*/ Console.ReadKey(); } } }
运行结果:
2、分支语句之swirch 语句
实例:
1、输入 1 显示为星期一,依此类推
程序如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5._2分支语句之switch语句 { class Program { static void Main(string[] args) { //输入 1 显示星期一,依次类推 Console.WriteLine("请输入1-7中的一个数字"); int week = int.Parse(Console.ReadLine()); //if (week == 1) Console.WriteLine("星期一"); //if (week == 2) Console.WriteLine("星期二"); //if (week == 3) Console.WriteLine("星期三"); //if (week == 4) Console.WriteLine("星期四"); //if (week == 5) Console.WriteLine("星期五"); //if (week == 6) Console.WriteLine("星期六"); //if (week == 7) Console.WriteLine("星期天"); 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(); } } }
运行结果:
2、判断2015年每个月份的天数
程序如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5._2分支语句之switch语句 { class Program { static void Main(string[] args) { //判断2015年每个月的天数 //31天:1、3、5、7、8、10、12 //30天:4、6、9、11 //28天:2 Console.WriteLine("请输入月份数字"); int month = int.Parse(Console.ReadLine()); switch(month) { case 2: Console.WriteLine("本月有28天"); break; case 4: case 6: case 9: case 11: Console.WriteLine("本月有30天"); break; default: Console.WriteLine("本月有31天"); break; } Console.ReadKey(); } } }
运行结果:
1、结构:
switch(表达式) { case 常量表达式: 条件语句; case 常量表达式: 条件语句; case 常量表达式: 条件语句; case 常量表达式: 条件语句; ...... default: 条件语句; }
3、分支语句之三位运算符
1、条件判断表达式?成立时执行的语句:不成立时的语句;
实例:
1)同样判断输入值与10 的关系;
2)< 10提示小于 10;
3)>= 10提示大于等于 10
三元运算符适用的条件:
只适用于判断具有两个结果的情况
分支语句之总结
1、if 语句:特别适合判断一些连续的值,可与 else 或 else if 配合适用;
2、switch 语句:特别适合判断一些离散的值。在使用的时候要注意每个 case 语句之后都必须有 break 语句。可配合 default 语句适用;
3、三元运算符:比较适用于简单的赋值语句,人适用于判断有两个结果的情况,可读性较差。
程序如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5._3_分支语句之三元运算符 { class Program { static void Main(string[] args) { //提示用户进行输入 Console.WriteLine("请输入您需要比较的数值"); int num = int.Parse(Console.ReadLine()); Console.WriteLine(num < 10 ? "您输入的数值10" : "您输入的数值大于等于10"); Console.ReadKey(); } } }
运行结果:
4、迭代语句之 while 语句
1、迭代语句是在程序中重复的执行,直到满足指定条件才停止的一段代码。当用户想重复执行某些语句时,可根据当前不同的任务,选择不同的循环语句适用,分别是:
while 语句 do while 语句 for 语句 foreach 语句
实例:
输出 1~50 这 50个数:
程序如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5._4迭代语句之_while_语句 { class Program { static void Main(string[] args) { //输出1~50 到屏幕上 int num = 1; while(num <= 50) { Console.WriteLine(num); num++; } Console.ReadKey(); } } }
运算结果:
2、while 语句的格式:
while(条件表达式) { 代码语句 }
3、while 语句在使用时,只有条件满足时才会被执行
5、迭代语句之do while 语句
1、使用格式:
do { 循环体语句; }while(判断语句);
do...while ;语句至少执行一次,即使 while 条件语句不成立时。
实例:
1)输出1~50 这50个数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5._5迭代语句之do__while_语句 { class Program { static void Main(string[] args) { //输出1 ~ 50 到屏幕上 int num = 0; do { num++; Console.WriteLine(num); } while (num < 50); Console.ReadLine(); } } }
运行结果:
2)计算现金存入银行多长时间才可以达到我们的预期收益(均按一年期定期存款,到期后自动转存)
本金 目标收益 利率 时间 (年)
一年的收益: 本金 X (1 + 利率)*1年 = 目标收益
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5._5_01 { class Program { static void Main(string[] args) { double Balance = 0; double Rate = 0; int Fear = 0; double TargetBalance = 0; Console.WriteLine("请输入您的本金:"); Balance = double.Parse(Console.ReadLine()); Console.WriteLine("请输入当前的利率百分比:"); Rate = double.Parse(Console.ReadLine())/100; Console.WriteLine("请输入您的目标收益:"); //验证输入的有效性 do { TargetBalance = double.Parse(Console.ReadLine()); if (TargetBalance <= Balance) Console.WriteLine("您没有必要存钱,请重新输入一个比本金大的收益:"); }while(TargetBalance<Balance); TargetBalance = double.Parse(Console.ReadLine()); do { Balance += (Rate+1); Fear++; } while (Balance < TargetBalance); Console.WriteLine("您将在{0}年内,获得{1}元的收益",Fear,Balance); Console.ReadLine(); } } }
运行结果:
6、迭代语句之 for 语句(1)
1、for 语句格式
for(初始条件;判断条件;循环条件) { 循环语句; }
for 循环可以记录循环的次数,有效控制的方式。
有时候,我们会省略初始条件,判断条件,循环条件,这时候的两个分号是不能省略。此时构成了一个死循环。
程序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5._6迭代语句之_for_语句 { class Program { static void Main(string[] args) { //求输入的阶乘 //1!=1 2!=2 x 1 Console.WriteLine("请输入需要计算阶乘的数:"); for (; ; ) { int a = int.Parse(Console.ReadLine()); int j = 1; for (int i = 1; i <= a; i++) { j *= i; } Console.WriteLine("阶乘的结果为" + j); //Console.ReadLine(); } } } }
运行结果:
7、迭代语句之 for 语句(2)
例题:
九九乘法表。
程序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5._7迭代语句之_for_语句 { class Program { static void Main(string[] args) { Console.WriteLine("********************九九乘法表*******************"); //使用 for 循环时,一般在 for 循环语句进行声明循环计次的变量 for(int i = 1; i < 10; i++) { for(int j = 1;j <= i; j++) { Console.Write("{0} x {1}={2} ",j,i,j*i); if (i * j < 10) Console.Write(" "); } Console.WriteLine(); } Console.ReadKey(); } } }
运行结果:
嵌套:就是套起来,将一个表格放在另一个表格中,称为表格的嵌套。循环嵌套,也就是一个循环中包含另一个循环。