C#整理4——循环语句

时间:2022-11-15 12:00:02

一、循环语句 

定义:可以反复执行某段代码,直到不满足循环条件为止。

循环的四要素:初始条件、循环条件、状态改变、循环体。

1.初始条件:循环最开始的状态。

2.循环条件:在什么条件下进行循环,不满足此条件,则循环终止。

3.状态改变:改变循环变量值,最终不满足循环条件,从而停止循环。

4.循环体:要反复执行的部分。

二、for循环(重点)

1. 语法:

for(表达式1;表达式2;表达式3)

{

执行语句;(循环体)

}

2. 执行过程:

1. 计算表达式1转向2

2. 计算表达式2(循环条件)若为true转向3,false转向5

3. 执行循环体转向4

4. 执行表达式3转向2

5. 循环结束

一般情况下:

表达式1:用于定义循环变量和对循环变量赋初值

表达式2:循环条件

表达式3:用于改变循环条件的值

注意:一般情况下for循环用于已知次数的循环中。

foreach循环

a. 语法:

foreach(数据类型 变量名 in 数组/集合)

{
                        循环体;

}

其中in关键字前面就是定义一个迭代变量,in后面就是实现了IEnumerable 或者 IEnumerable<T> 接口的对象

b. foreach遍历数组的简单原理:“in数组名”会将数组中的元素从第0个开始到最后一个遍历出来赋给迭代变量,所以迭代变量直接就是数组元素的值。

注意:迭代变量的数据类型必须要与数组中元素类型一致。

c. 执行过程:每次遍历的时候将当前遍历出来的数组元素拿出来赋给迭代变量,然后再执行循环体。

d. for循环和foreach循环的区别与联系:

1. 相同点:

都可以遍历数组中的元素。

2. 不同点:

1> for循环遍历出来的是数组元素的下标,foreach遍历出来的直接就数组元素。

2> for循环可以从指定下标开始遍历,而foreach只能从第0个开始直到最后一个结束。

3> 在for循环中可以更改遍历出来元素的值,而在foreach中不能更改遍历出来的值(迭代变量的值)。

4> for循环中可以得到当前遍历出来的值以外的值(即上一个值和下一个值,因为for是通过遍历索引来取值的)而foreach只能得到当前遍历出来的元素值。

因为foreach是直接遍历数组中元素的值。

e. 什么情况下用foreach来遍历数组中的元素?

1. 从头开始遍历直到最后一个。

2. 只取值不改值

while循环

1.语法:

while(循环条件)

{

循环体;

}

1. 执行过程:

1. 先判断循环条件,如果为true。则转向2,如果为false转向语句3

2. 执行循环体,执行完了转向1。

3. 跳出循环循环结束。

注意:在while循环中一定要有那么一句话来改变循环变量的值,使循环变量终有那么一个时候为false。(不然死循环)

do-while()循环

1. 语法:

do

{

//循环体

}while(循环条件);注意:这里一定要有分号。

2.执行过程:

1.执行循环体,执行完循环体转向2

2.判断条件是否成立,如果条件为true则转向1 为false则转向3.

3.跳出循环,循环结束。

3.while与do-while的区别:

如果条件一开始就为False,对于while循环,循环体一次都不会执行。对于do-while循环体执行一次。即:while先判断后执行,do-while先执行一次再判断。

三、执行过程:

1.执行初始条件

2.执行循环条件

3.循环体

4.状态改变

5继续第2步。

四、for 循环的应用:迭代法,穷举法。

迭代法应用:

1.100以内所有数的和。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
//求前100个数的和
static void Main(string[] args)
{
int sum = ;
for (int i = ; i <= ; i++)
{
sum = sum + i;
}
Console.WriteLine(sum);
}
}
}

2.求阶乘

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class1
{
//求阶乘 5! = 5*4*3*2*1 = 5*4!;
static void Main(string[] args)
{
int jc = ;
for(int i=;i<=;i++)
{
jc = jc * i;
}
Console.WriteLine(jc);
}
}
}

3.求年龄:有6个小孩子排在一起,问第一个多大年龄,他说比第二个小2岁,问第二个多大年龄,他说比第三个小2岁,以此类推,问第6个多大年龄,他说自己16岁。问第一个小孩子几岁?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
//求年龄:有6个小孩子排在一起,问第一个多大年龄,他说比第二个小2岁,问第二个多大年龄,他说比第三个小2岁,以此类推,问第6个多大年龄,他说自己16岁。问第一个小孩子几岁?
class Class2
{
static void Main(string[] args)
{
int age = ; //初始情况下,存的是第6个小孩子年龄,每次循环都会减2,分别代表第5,4,3,2,1个小孩子的年龄。
for (int i = ; i >= ; i--)
{
age = age - ;
}
Console.WriteLine(age);
}
}
}

4.折纸:a.一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,问折多少次能超过珠峰的高度?

            b.一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,折50次高度是多少?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
//一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,问折多少次能超过珠峰的高度?
//一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,折50次高度是多少?
class Class3
{
static void Main(string[] args)
{
double h = 0.00015;
for (int i = ; i <= ; i++)
{
h = h * ;
}
Console.WriteLine(h);
}
}
}

5.棋盘放粮食( 自己做)

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class 棋盘粮食
{
static void Main(string[] args)
{
int sum = , s = ;
for (int i = ; i <= ;i++ )
{
s = s * ;
sum = sum+s;
}
Console.WriteLine(sum); }
}
}

6.猴子吃桃子:公园里有一只猴子,和一堆桃子。猴子每天吃掉桃子数量的一半,把剩下的一半数量中扔掉一个坏的。到了第7天猴了睁开眼发现只剩下一个桃子了,问原来有多。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
//猴子吃桃子。
//公园里有一只猴子,和一堆桃子。猴子每天吃掉桃子数量的一半,把剩下的一半数量中扔掉一个坏的。到了第7天猴了睁开眼发现只剩下一个桃子了,问原来有多。
//190
class Class4
{
static void Main(string[] args)
{
int count = ;
for(int i=;i>=;i--)
{
count = (count + ) * ;
}
Console.WriteLine(count);
}
}
}

7.落球问题。(自己做)一个球从10米高度落下,每次弹起2/3的高度。问第五次弹起后的高度是多少?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class 落球问题
{
//一个球从10米高度落下,每次弹起2/3的高度。问第五次弹起的高度是多少?
static void Main(string[] args)
{
double h = ;
for (int i = ; i <= ;i++ )
{
h = h * / ;
}
Console.WriteLine(h);
}
}
}

8.兔子小兔子的问题。一对新生兔,到三个月开始生一对小兔,以后每个月都会生一对小兔,小兔不断长大也会生小兔。假设兔子不死,每次只能生一对(公母),问第24末有多少只兔子?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
//兔子生兔子
class Class5
{
static Main fff(string[] args)
{
int tu1 = , tu2 = ; //tu1是倒数第一个月的兔子数,tu2是倒数第二个月的兔子数
int tu=;//要求的这个月的兔子数。 for (int i = ; i <= ; i++)
{
tu = tu1 + tu2;
tu2 = tu1;
tu1 = tu; }
Console.WriteLine(tu);
}
}
}

穷举法的应用:用循环把各种可能的情况都给走一遍,然后用if条件把满足要求的结果给筛选出来。

1.找100以内的与7有关的数。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for (int i = ; i <= ; i++)
{ if (i % == || i % == || i / == ) //重点
{ Console.Write(i + "\t"); } }
}
}
}

2.有三种硬币若干:1分,2分,5分。要组合1毛5,有哪些组合方式?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class6
{
static void Main(string[] args)
{
for(int a=;a<=;a++) //a代表1分的硬币个数
{
for(int b=;b<=;b++)//b代表2分的硬币个数
{
for(int c=;c<=;c++)//c代表5分硬币的个数
{
if(a*+b*+c* == )
{
Console.WriteLine("1分硬币需要"+a+"个,2分的硬币需要"+b+"个,5分的硬币需要"+c+"个");
}
}
}
}
}
}
}

3.买东西。小张过元旦发了100元的购物券,他要买香皂(5元),牙刷(2元),洗发水(20元)。要想把100元正好花完,如何买这三样东西?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
class 元旦
{
//购物券100元,香皂(5元),牙刷(2元),洗发水(20元)。正好花完,怎么花?
static void Main(string[] args)
{
for (int a=;a<=;a++)
{
for (int b = ; b <= ; b++)
{
for (int c = ; c <= ;c++ )
{
if (*a+*b+*c==)
{
Console.WriteLine("应该买香皂"+a+"块,牙刷"+b+"只,洗发水"+c+"瓶。");
}
}
}
}
}
}
}

4.百鸡百钱。有100文钱,要买100只鸡回家。公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只。如何买?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
class 百钱白鸡
{
//有100文钱,要买100只鸡回家。公鸡2文钱一只,母鸡一文钱一只,小鸡半文钱一只。如何买?
static void Main(string[] args)
{
for (int a = ; a <= ;a++ )
{
for (int b = ; b <= ;b++ )
{
for (double c = ; c <= ;c++ )
{
if(a*+b*+c*0.5==&&a+b+c==)
{
Console.WriteLine("公鸡"+a+"只,母鸡"+b+"只,小鸡"+c+"只。");
}
}
}
}
}
}
}

百马百石。有100石粮食,母匹大马驮2石,每匹中马驮1石,每两匹小马驹一起驮1石。要用100匹马驮完100石粮食,如何按排?

5.某侦察队接到一项紧急任务,要求在A、B、C、D、E、F六个队员中尽可能多地挑若干人,但有以下限制条件:
A和B两人中至少去一人; a+b>=1
A和D不能一起去; a+d<=1
A、E和F三人中要派两人去; a+e+f==2
B和C都去或都不去; b+c!=1
C和D两人中去一个; c+d==1
若D不去,则E也不去。 d+e==0||d==1
问应当让哪几个人去?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
class 侦察队
{
static void Main(string[] args)
{
for (int a = ; a <= ;a++ )
{
for (int b = ; b <= ;b++ )
{
for (int c = ; c <= ;c++ )
{
for (int d = ; d <= ;d++ )
{
for (int e = ; e <= ;e++ )
{
for (int f = ; f <= ;f++ )
{
if (a + b >= && a + d <= && a + e + f == && b + c != && c + d == && (d + e == || d == ))
{
Console.WriteLine("a="+a+";b="+b+";c="+c+";d="+d+";e="+e+";f="+f);
}
}
}
}
}
} }
}
}
}

6.123()45()67()8()9=100;要求在()里面填写+或-使等式成立。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
class Class1
{
//123()45()67()8()9=100;要求在()里面填写+或-使等式成立。
static void Main(string[] args)
{
for (int a = -; a <= ;a=a+ )//-1代表减号,1代表加号
{
for (int b = -; b <= ;b=b+ )
{
for (int c = -; c <= ;c=c+ )
{
for (int d = -; d <= ;d=d+ )
{
if (+a*+*b+*c+*d==)
{
Console.WriteLine("a="+a+";b="+b+";c="+c+";d="+d);
}
}
}
}
}
}
}
}