C#代码教程之循环的介绍

循环

while 循环
当给定条件为真时,重复语句或语句组。他会在执行循环主体之前测试条件。
int a = 10;
while (a < 13)
{
    Console.WriteLine("a 的值:" + a);
    a++;
}
 
输出结果为
a 的值: 10
a 的值: 11
a 的值: 12
for / foreach 循环
多次执行一个语句序列,简化管理循环变量的代码。
for (int a = 10; a < 13; a = a + 1)
{
    Console.WriteLine("a 的值:" + a);
}
输出结果为
a 的值: 10
a 的值: 11
a 的值: 12
 

无限循环

 
for (; ; )
{
    Console.WriteLine("888");
}
 
do while 循环
除了它是在循环主体结尾测试条件外,其他与  while 语句类似。
int a = 10;
do
{
    Console.WriteLine("a 的值:" + a);
    a = a + 1;
} while (a < 13);
输出结果为
a 的值: 10
a 的值: 11
a 的值: 12
嵌套循环
可以在  while for do while 循环内使用一个或多个循环。
 
嵌套 for循环
嵌套while循环
嵌套 do while循环
for ( init ; condition ; increment )
{
for ( init ; condition ; increment )
{
statement ( s );
}
statement ( s );
}
while ( condition )
{
while ( condition )
{
statement ( s );
}
statement ( s );
}
do
{
statement ( s );
do
{
statement ( s );
} while ( condition );
} while ( condition );

循环控制

break 语句
语法:break ;
终止循环,执行下一条语句。
cintinue 语句
语法: continue ;
跳出当前循环,执行下一循环。
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值