循环
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
;
|
跳出当前循环,执行下一循环。
|