c中的while循环递减

发布于 2024-12-07 11:01:37 字数 246 浏览 0 评论 0原文

是否可以在 C 中的 while 循环中将数组大小减少超过 x--.例如,您可以在每次迭代时将数组减少数组大小的三分之一吗?

int n = 10;

while (n < 0)

// do something

(round(n/3))-- // this doesn't work, but can this idea be expressed in C?

谢谢您的帮助!

Is it possible to decrement the array size in a while loop in C by more than x--. For example, can you decrement an array by a third of the array size with each iteration?

int n = 10;

while (n < 0)

// do something

(round(n/3))-- // this doesn't work, but can this idea be expressed in C?

Thank you for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

街道布景 2024-12-14 11:01:37

您可以使用任何表达式:

int n = 10;
while (n > 0)   // Note change compared with original!
{
    // Do something
    n = round(n/3.0) - 1;  // Note assignment and floating point
}

请注意,您只能递减变量,而不能递减表达式。

您还可以使用 for 循环:

for (int n = 10; n > 0; n = round(n/3.0) - 1)
{
    // Do something
}

在这种情况下,n 的值序列将相同 (n = 10, 2)无论您是否使用浮点进行舍入,因此您都可以编写:

n = n / 3 - 1;

并且您会看到相同的结果。对于其他上限,序列将发生变化 (n = 11, 3)。两种技术都很好,但是您需要确保您知道自己想要什么,仅此而已。

You can use any expression:

int n = 10;
while (n > 0)   // Note change compared with original!
{
    // Do something
    n = round(n/3.0) - 1;  // Note assignment and floating point
}

Note that you can only decrement variables, not expressions.

You could also use a for loop:

for (int n = 10; n > 0; n = round(n/3.0) - 1)
{
    // Do something
}

In this case, the sequence of values for n will be the same (n = 10, 2) whether you round using floating point or not, so you could write:

n = n / 3 - 1;

and you'd see the same results. For other upper limits, the sequence would change (n = 11, 3). Both techniques are fine, but you need to be sure you know what you want, that's all.

困倦 2024-12-14 11:01:37

是的,可以向变量 n 添加或减去任何数字。

通常,如果您想要以非常可预测的次数执行某件事,则可以使用 for 循环;当您不确定某件事会发生多少次,而是测试某种条件时,您可以使用 while 循环。

最罕见的循环是 do / while 循环,仅当您想在第一次 while 之前执行一次循环时才使用它代码>检查发生。

示例:

// do something ten times
for (i = 0; i < 10; ++i)
    do_something();

// do something as long as user holds down button
while (button_is_pressed())
    do_something();

// play a game, then find out if user wants to play again
do
{
    char answer;
    play_game();
    printf("Do you want to play again?  Answer 'y' to play again, anything else to exit. ");
    answer = getchar();
} while (answer == 'y' || answer == 'Y');

Yes, it is possible to add or subtract any number to your variable n.

Usually, if you want to do something a very predictable number of times, you would use a for loop; when you aren't sure how many times something will happen, but rather you are testing some sort of condition, you use a while loop.

The rarest loop is a do / while loop, which is only used when you want to execute a loop one time for certain before the first time the while check occurs.

Examples:

// do something ten times
for (i = 0; i < 10; ++i)
    do_something();

// do something as long as user holds down button
while (button_is_pressed())
    do_something();

// play a game, then find out if user wants to play again
do
{
    char answer;
    play_game();
    printf("Do you want to play again?  Answer 'y' to play again, anything else to exit. ");
    answer = getchar();
} while (answer == 'y' || answer == 'Y');
夏夜暖风 2024-12-14 11:01:37

您的代码中没有数组。如果您不希望 n 在每次迭代中获得其值的三分之一,则可以执行 n /= 3;。请注意,由于 n 是整数,因此将应用积分除法。

There is no array in your code. If you wan't n to have a third of its value on each iteration, you can do n /= 3;. Note that since n is integral then the integral division is applied.

走走停停 2024-12-14 11:01:37

正如 K-Ballo 所说,示例代码中没有数组,但这里有一个带有整数数组的示例。

int n = 10;
int array[10];
int result;

// Fill up the array with some values
for (i=0;i<n;i++)
    array[i] = i+n;

while(n > 0)
{
    // Do something with array

    n -= sizeof(array)/3;
}

但要小心,在您给出的示例代码中,while 循环正在检查 n 是否小于零。由于 n 被初始化为 10,因此 while 循环将永远不会被执行。我在我的例子中改变了它。

Just like K-Ballo said there is no array in your example code but here is an example with an integer array.

int n = 10;
int array[10];
int result;

// Fill up the array with some values
for (i=0;i<n;i++)
    array[i] = i+n;

while(n > 0)
{
    // Do something with array

    n -= sizeof(array)/3;
}

But be careful in the example code you gave the while loop is checking if n is less than zero. As n is intialised to 10 the while loop will never be executed. I have changed it in my example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文