C#的语法----程序结构(4)

下面我们学习一个循环结构

while循环

语法:

while(循环条件)

{

   循环体;

}

执行过程:程序运行到while处,首先判断while所带的小括号内的循环条件是否成立,成立返回true,则执行循环体,执行完

          一遍后再次判断循环条件,成立继续执行循环体,若不再成立时,咋跳出。

下面看一个小练习:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Panel
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //求1-100之间所有整数的和
14             //循环体:累加的过程
15             //循环条件:i<=100
16             int i = 1;
17             int sum = 0;
18             while (i<=100)
19             {
20                 sum += i;
21                 i++;
22             }
23             Console.WriteLine(sum);
24             Console.ReadKey();
25         }
26     }
27 }

下面讲一下一个很重要的关键字break

(1)可以跳出switch-case结构

(2)跳出当前循环。

请看下面一个例子

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Panel
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int i = 1;
14             int j = 1;
15             while (i<=10)
16             {
17                 while (j<=10)
18                 {
19                     Console.WriteLine("我是里面的while循环");
20                     j++;
21                     break;
22                 }
23                 Console.WriteLine("我是外面的while循环");
24                 i++;
25             }
26             Console.ReadKey();
27         }
28     }
29 }

这个例子很好的说明了break的作用

 break一般不单独使用,而是和if判断一起使用,表示,当满足某些条件的时候,就不再循环了。

大家来练习几个代码

练习1

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Panel
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string userNmae = string.Empty;
14             string userPwd = string.Empty;
15             while (userNmae != "admin" || userPwd != "888888")
16             {
17                 Console.WriteLine("请输入用户名");
18                 userNmae = Console.ReadLine();
19                 Console.WriteLine("请输入密码");
20                 userPwd = Console.ReadLine();
21             }
22             Console.WriteLine("登陆成功");
23             Console.ReadKey();
24         }
25     }
26 }

练习2:输入班级人数,然后依次输入学员成绩,计算班级平均成绩和总成绩。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Panel
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("请输入班级人数");
14             int count = Convert.ToInt32(Console.ReadLine());
15             int i = 1;
16             int sum = 0;
17 
18             while (i <= count)
19             {
20                 Console.WriteLine("请输入第{0}个学院考试成绩",i);
21                 int score = Convert.ToInt32(Console.ReadLine());
22                 sum += score;
23             }
24             Console.WriteLine("{0}个人的班级总成绩是{1}平均成绩{2}",count,sum,sum/count);
25             Console.ReadKey();
26         }
27     }
28 }

猜你喜欢

转载自www.cnblogs.com/LiyuLi/p/12080336.html