c语言 循环嵌套循环_如何使用C中的循环

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.

我们首先定义一个循环变量,在本例中为ii是用于循环的通用变量名, 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中有这种循环是很常见的。

翻译自: https://flaviocopes.com/c-loops/

c语言 循环嵌套循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值