Chapter 2. C#语句---循环语句

一、for循环 

四要素:

初始条件  循环条件  状态改变  循环体

格式:

for(初始条件;循环条件;状态改变)

{

   循环体

}

break:终止当前及以后的循环

continue:终止当前循环,进行下一循环

 1  //穷举:
 2             //百钱白鸡:公鸡2文钱,母鸡1文钱,小鸡1文钱三只,100文钱各买多少只
 3             int count = 0;
 4             for (int gong = 0; gong <= 50; gong++) 
 5             {
 6                 for (int mu = 0; mu <= 100; mu++) 
 7                 {
 8                     if((gong*2+mu*1+(100-gong-mu)/3==100)&&(100-gong-mu)%3==0)
 9                     {
10                         count++;
11                         Console.WriteLine("买公鸡"+gong+"只,母鸡"+mu+"只,小鸡"+(100-gong-mu)+"只。");
12                     }
13                 }
14             }
15             Console.WriteLine("共有"+count+"种买法");
16             Console.ReadLine();

 1  迭代:       /*
 2              五个小朋友排成一队,第一个比第二个大两岁,第二个比第三个大两岁,
 3              以此类推,最后一个3岁,问第一个几岁?
 4              */
 5             int x = 3;
 6             for (int a = 1; a < 5;a++ )
 7             {
 8                  x = x + 2;
 9                 Console.WriteLine("a="+a+" x="+x);
10             }
11             
12             Console.ReadLine();

二、while循环

 1 //迭代:
 2             //1、纸张厚度0.07mm,对折多少次达到8848m
 3             int times = 0;
 4             double h = 0.07;
 5             while (h <= 8848000)
 6             {
 7                 h = h * 2;
 8                 times++;
 9                 Console.WriteLine("折纸第" + times + "次,厚度为:" + h + "m");
10             }
11             Console.WriteLine("折纸" + times + "次,达到8848m");
12             Console.ReadLine();

原文地址:https://www.cnblogs.com/xiao55/p/5458579.html