在C ++中有没有办法只返回for循环的最后一个实例?

时间:2022-08-28 08:34:20

Like for example:

例如:

#include <iostream>
using namespace std;
int main()
{
    for (int n=10; n>0; n--){
        cout<< n <<", ";}
}

This will output the numbers 10,9,8,7,6,5,4,3,2,1 So is there a new way so I just get the last instance of the loop, the 1? I new at this and google isn't giving me any answers.

这将输出数字10,9,8,7,6,5,4,3,2,1所以有一种新方法,所以我只得到循环的最后一个实例,1?我是新来的,谷歌没有给我任何答案。

4 个解决方案

#1


There is no direct way to detect whether the current iteration of a for loop is the last one. But if the behavior of the loop is predictable, you can usually write code that can detect when you're on the last iteration.

没有直接的方法来检测for循环的当前迭代是否是最后一个迭代。但是如果循环的行为是可预测的,那么通常可以编写可以检测到最后一次迭代的代码。

In this case, you could do something like:

在这种情况下,您可以执行以下操作:

if (n == 1) {
    cout << n << "\n";
}

in the body of the loop. (Of course it would be simpler in this case to replace the entire loop with cout << "1\n";, but I presume this is an example of something more complex.)

在循环的身体。 (当然,在这种情况下用cout <<“1 \ n”;替换整个循环会更简单,但我认为这是一个更复杂的例子。)

In more complicated cases, you can save whatever information you need in the body of the loop:

在更复杂的情况下,您可以在循环体中保存所需的任何信息:

int value_to_print:
for ( ... ) {
    value_to_print = i;
}
std::cout << value_to_print << "\n";

On each iteration, value_to_print is replaced by the current value of i. The final value is the value of i on the last iteration.

在每次迭代中,value_to_print将替换为i的当前值。最终值是最后一次迭代的i值。

#2


You could create a variable (outside the loop) to hold the "current" value of n; whatever happens to the loop (exit condition reached, break, an exception is thrown...) the value will stay there:

您可以创建一个变量(在循环外)来保存n的“当前”值;无论发生什么循环(达到退出条件,中断,抛出异常...),该值将保持在那里:

int last_n;
for (int n=10; n>0; n--) {
    last_n = n;
    cout<< n <<", ";

    if (something) {
        break;                   // works in this case
    } else if (something else) {
        throw some_random_error; // works in this case too
    }
}

cout << "The last value of 'n' was " << last_n << endl;

#3


You can use a simple if statement for that.

您可以使用简单的if语句。

int main()
{
    for (int n=10; n>0; n--) {
        cout << n << ", ";
        if( n == 1 ) {
            return n;
        }
    }
}

#4


The simplest way to accomplish this is: -

实现这一目标的最简单方法是: -

#include <iostream>
using namespace std;
int main()
{
    int x;
    for (int n = 10; n > 0; n--){
        x = n;
    }
    cout << x;
    return 0;
}

#1


There is no direct way to detect whether the current iteration of a for loop is the last one. But if the behavior of the loop is predictable, you can usually write code that can detect when you're on the last iteration.

没有直接的方法来检测for循环的当前迭代是否是最后一个迭代。但是如果循环的行为是可预测的,那么通常可以编写可以检测到最后一次迭代的代码。

In this case, you could do something like:

在这种情况下,您可以执行以下操作:

if (n == 1) {
    cout << n << "\n";
}

in the body of the loop. (Of course it would be simpler in this case to replace the entire loop with cout << "1\n";, but I presume this is an example of something more complex.)

在循环的身体。 (当然,在这种情况下用cout <<“1 \ n”;替换整个循环会更简单,但我认为这是一个更复杂的例子。)

In more complicated cases, you can save whatever information you need in the body of the loop:

在更复杂的情况下,您可以在循环体中保存所需的任何信息:

int value_to_print:
for ( ... ) {
    value_to_print = i;
}
std::cout << value_to_print << "\n";

On each iteration, value_to_print is replaced by the current value of i. The final value is the value of i on the last iteration.

在每次迭代中,value_to_print将替换为i的当前值。最终值是最后一次迭代的i值。

#2


You could create a variable (outside the loop) to hold the "current" value of n; whatever happens to the loop (exit condition reached, break, an exception is thrown...) the value will stay there:

您可以创建一个变量(在循环外)来保存n的“当前”值;无论发生什么循环(达到退出条件,中断,抛出异常...),该值将保持在那里:

int last_n;
for (int n=10; n>0; n--) {
    last_n = n;
    cout<< n <<", ";

    if (something) {
        break;                   // works in this case
    } else if (something else) {
        throw some_random_error; // works in this case too
    }
}

cout << "The last value of 'n' was " << last_n << endl;

#3


You can use a simple if statement for that.

您可以使用简单的if语句。

int main()
{
    for (int n=10; n>0; n--) {
        cout << n << ", ";
        if( n == 1 ) {
            return n;
        }
    }
}

#4


The simplest way to accomplish this is: -

实现这一目标的最简单方法是: -

#include <iostream>
using namespace std;
int main()
{
    int x;
    for (int n = 10; n > 0; n--){
        x = n;
    }
    cout << x;
    return 0;
}