for (int i = number_1; i > 0; i--)
{
int j = 2;
printf("%*s", i - 1,"");
for (j; j > 0; j--)
{
printf("#");
}
printf("\n");
j = j + 1;
}
Here I'm trying to run my secondary for loop by taking a variable that is declared in the previous loop. It iterates and prints the symbol "#", then gets incremented again by 1 before the outer loop ends.
在这里,我尝试使用前一个循环中声明的变量来运行第二个for循环。它迭代并打印符号“#”,然后在外部循环结束之前再增加1。
I'm getting this error:
我得到这个错误:
expression result unused
表达式结果未使用
What's going on here? I can certainly accomplish the task in a different way. But I'd like to find out why the code doesn't work in this case.
这是怎么回事?我当然可以用不同的方式完成任务。但是我想知道为什么在这种情况下代码不能工作。
3 个解决方案
#1
3
this statement:
这句话:
j = j + 1;
gives 'j' a value, but the next iteration through the loop hits this statement:
给'j'一个值,但循环的下一个迭代会遇到以下语句:
int j = 2;
So the results of the first statement are not used.
所以第一个语句的结果没有被使用。
However, since it is 'initialization', that might not matter.
但是,由于是“初始化”,这可能并不重要。
Then this statement:
然后这句话:
for (j; j > 0; j--)
has an error in the syntax. Where the first parameter is used to initialize a value, (in this case 'j') but no initialization is performed.
在语法中有错误。第一个参数用于初始化一个值(在本例中为'j'),但不执行初始化。
Suggest:
建议:
for (; j > 0; j--)
so the first parameter is omitted
因此省略了第一个参数
#2
1
change to
改变
for (int i = number_1, j = 2; i > 0; i--, ++j)//j move to this.
{
printf("%*s", i - 1,"");
for (int k = j; k > 0; k--)
{
printf("#");
}
printf("\n");
}
#3
0
The error is usually only a warning. You have the expression j;
here:
错误通常只是一个警告。有一个表达式j;在这里:
for (j; j > 0; j--)
but you don't assign the value of j
to anything.
但是你没有把j的值赋给任何东西。
Change it to
将其更改为
for (; j > 0; j--)
And then there is another problem with j
, as @user3629249 pointed out.
Move the declaration of j
outside the loop:
还有另一个问题是j,@user3629249指出。移动j外循环的宣言:
int j = 2;
for (int i = number_1; i > 0; i--)
{
printf("%*s", i - 1,"");
for (; j > 0; j--)
{
printf("#");
}
printf("\n");
j = j + 1;
}
#1
3
this statement:
这句话:
j = j + 1;
gives 'j' a value, but the next iteration through the loop hits this statement:
给'j'一个值,但循环的下一个迭代会遇到以下语句:
int j = 2;
So the results of the first statement are not used.
所以第一个语句的结果没有被使用。
However, since it is 'initialization', that might not matter.
但是,由于是“初始化”,这可能并不重要。
Then this statement:
然后这句话:
for (j; j > 0; j--)
has an error in the syntax. Where the first parameter is used to initialize a value, (in this case 'j') but no initialization is performed.
在语法中有错误。第一个参数用于初始化一个值(在本例中为'j'),但不执行初始化。
Suggest:
建议:
for (; j > 0; j--)
so the first parameter is omitted
因此省略了第一个参数
#2
1
change to
改变
for (int i = number_1, j = 2; i > 0; i--, ++j)//j move to this.
{
printf("%*s", i - 1,"");
for (int k = j; k > 0; k--)
{
printf("#");
}
printf("\n");
}
#3
0
The error is usually only a warning. You have the expression j;
here:
错误通常只是一个警告。有一个表达式j;在这里:
for (j; j > 0; j--)
but you don't assign the value of j
to anything.
但是你没有把j的值赋给任何东西。
Change it to
将其更改为
for (; j > 0; j--)
And then there is another problem with j
, as @user3629249 pointed out.
Move the declaration of j
outside the loop:
还有另一个问题是j,@user3629249指出。移动j外循环的宣言:
int j = 2;
for (int i = number_1; i > 0; i--)
{
printf("%*s", i - 1,"");
for (; j > 0; j--)
{
printf("#");
}
printf("\n");
j = j + 1;
}