c语言 循环嵌套循环
C offers us three ways to perform a loop: for loops, while loops and do while loops. They all allow you to iterate over arrays, but with a few differences. Let’s see them in details.
C为我们提供了三种执行循环的方法: for循环 , while循环和do while循环 。 它们都允许您遍历数组,但有一些区别。 让我们详细了解它们。
对于循环 (For loops)
The first, and probably most common, way to perform a loop is for loops.
执行循环的第一种方法(可能是最常见的方法)是for循环 。
Using the for
keyword we can define the rules of the loop up front, and then provide the block that is going to be executed repeatedly.
使用for
关键字,我们可以预先定义循环规则 ,然后提供将要重复执行的块。
Like this:
像这样:
for (int i = 0; i <= 10; i++) {
/* instructions to be repeated */
}
The (int i = 0; i <= 10; i++)
block contains 3 parts of the looping details:
(int i = 0; i <= 10; i++)
块包含3部分循环详细信息:
the initial condition (
int i = 0
)初始条件(
int i = 0
)the test (
i <= 10
)测试(
i <= 10
)the increment (
i++
)增量(
i++
)
We first define a loop variable, in this case named i
. i
is a common variable name to be used for loops, along with j
for nested loops (a loop inside another loop). It’s just a convention.
我们首先定义一个循环变量,在本例中为i
。 i
是用于循环的通用变量名, j
是嵌套循环(另一个循环内的循环)的名称。 这只是一个约定。
The variable is initialized at the 0 value, and the first iteration is done. Then it is incremented as the increment part says (i++
in this case, incrementing by 1), and all the cycle repeats until you get to the number 10.
将该变量初始化为0值,并完成第一次迭代。 然后按照增量部分的说明进行递增(在本例中为i++
,递增1),并且所有循环重复进行,直到达到数字10。
Inside the loop main block we can access the variable i
to know at which iteration we are. This program should print 0 1 2 3 4 5 5 6 7 8 9 10
:
在循环主块内部,我们可以访问变量i
来知道我们在哪个迭代中。 该程序应打印0 1 2 3 4 5 5 6 7 8 9 10
:
for (int i = 0; i <= 10; i++) {
/* instructions to be repeated */
printf("%u ", i);
}
Loops can also start from a high number, and go a lower number, like this:
循环也可以从较高的数字开始,而从较低的数字开始,如下所示:
for (int i = 10; i > 0; i--) {
/* instructions to be repeated */
}
You can also increment the loop variable by 2 or another value:
您还可以将循环变量增加2或另一个值:
for (int i = 0; i < 1000; i = i + 30) {
/* instructions to be repeated */
}
While循环 (While loops)
While loops is simpler to write than a for
loop, because it requires a bit more work on your part.
While循环比for
循环更容易编写,因为它需要您做更多的工作。
Instead of defining all the loop data up front when you start the loop, like you do in the for
loop, using while
you just check for a condition:
取而代之的定义所有的回路数据前面,当你开始循环,就像你在做for
循环使用while
你只是检查条件:
while (i < 10) {
}
This assumes that i
is already defined and initialized with a value.
假设i
已经定义了i
并使用值进行了初始化。
And this loop will be an infinite loop unless you increment the i
variable at some point inside the loop. An infinite loop is bad because it will block the program, nothing else can happen.
除非您在循环内的某个点增加i
变量,否则该循环将是无限循环。 无限循环是不好的,因为它将阻塞程序,其他任何事情都不会发生。
This is what you need for a “correct” while loop:
这是“正确”的while循环所需要的:
int i = 0;
while (i < 10) {
/* do something */
i++;
}
There’s one exception to this, and we’ll see it in one minute. Before, let me introduce do while
.
对此有一个例外,我们将在一分钟内看到它。 之前,我先介绍一下do while
。
做while循环 (Do while loops)
While loops are great, but there might be times when you need to do one particular thing: you want to always execute a block, and then maybe repeat it.
虽然循环是伟大的,但有可能当你需要做一个特别的事情是次:始终要执行的块,然后可能重复。
This is done using the do while
keyword, in a way that’s very similar to a while
loop, but slightly different:
这是使用do while
关键字完成的,其方式与while
循环非常相似,但略有不同:
int i = 0;
do {
/* do something */
i++;
} while (i < 10);
The block that contains the /* do something */
comment is always executed at least once, regardless of the condition check at the bottom.
不管底部是否进行条件检查,包含/* do something */
注释的块始终至少执行一次。
Then, until i
is less than 10, we’ll repeat the block.
然后,直到i
小于10,我们将重复该块。
使用break
循环 (Breaking out of a loop using break
)
In all the C loops we have a way to break out of a loop at any point in time, immediately, regardless of the conditions set fo the loop.
在所有C循环中,无论循环设置的条件如何,我们都可以在任何时间点立即退出循环。
This is done using the break
keyword.
这是使用break
关键字完成的。
This is useful in many cases. You might want to check for the value of a variable, for example:
在许多情况下这很有用。 您可能要检查变量的值,例如:
for (int i = 0; i <= 10; i++) {
if (i == 4 && someVariable == 10) {
break;
}
}
Having this option to break out of a loop is particularly interesting for while
loops (and do while
too), because we can create seemingly infinite loops that end when a condition occurs, and you define this inside the loop block:
对于while
循环(也do while
),具有此选项可以使循环中断特别有趣,因为我们可以创建看似无限的循环,当条件发生时结束,并在循环块内定义此循环:
int i = 0;
while (1) {
/* do something */
i++;
if (i == 10) break;
}
It’s rather common to have this kind of loops in C.
在C中有这种循环是很常见的。
c语言 循环嵌套循环