I am working on that code, and I tried to use x + 1
instead of ++x
and the result was infinite loop and zeros were printed on the screen.
我正在处理这段代码,我尝试使用x + 1而不是++x,结果是无限循环,屏幕上显示0。
this is the code:
这是代码:
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x + 1) //instead of ++x
printf("%d\t",x);
return 0;
}
I wondered this action.....why the compiler didn't produce such an error,,,, and what actually happens??!! and is x++
is treated as x += 1
?!
我想知道这个动作.....为什么编译器没有产生这样的错误,以及实际发生了什么?x++是x+ = 1吗?
4 个解决方案
#1
9
You need the value of x
to change, or your loop will never terminate. When you have only x + 1
, then at the end of the iteration, x + 1
is computed, but its resulting value is discarded. The result of the expression is not stored any place. So x
will remain zero, and x <= 100
will remain true, and your loop will continue forever.
您需要更改x的值,否则您的循环将永远不会终止。当只有x + 1时,在迭代结束时,计算出x + 1,但其结果值被丢弃。表达式的结果不存储在任何位置。所以x仍然是0,x <= 100仍然是正确的,你的循环会一直持续下去。
If you have any of the following:
如你有下列任何一项:
x = x + 1
x += 1
x++
++x
the value of x
itself is incremented. That's what you need.
x本身的值是递增的。这是你需要的。
#2
6
Although the basic structure of a for loop is like-
虽然for循环的基本结构是-
for(initialization; condition; increment or decrement)
But in its core part the "condition" part is treated only to control the loop behavior. So, if other two parts are syntactically correct, then compiler won't produce any error.
但在其核心部分,“条件”部分仅用于控制循环行为。因此,如果其他两个部分在语法上是正确的,那么编译器不会产生任何错误。
Since, x+1
is a valid C statement and the value of the x
is not changing so it will go to an infinite loop for the condition [x <= 100 => 0 <= 100
] being true forever.
因为x+1是一个有效的C语句,并且x的值没有变化,所以它会进入一个无限循环,条件是[x <= 100 => <= 100]永远为真。
Again, x++
or ++x
both treated as x = x + 1
when used independently. But, x++
is actually Post-increment operator, while ++x
is Pre-increment operator. That means, the with ++x
, the value of the x
will be incremented first and then assigned to x
. Whereas, the current value of x
will be used for the entire statement in x++
and then x
will be incremented and assigned with new value. Look at the following example-
同样,单独使用时,x++或++x都被视为x = x+ 1。但是,x++实际上是后增量运算符,而++x是预增量运算符。这意味着,++x的值,x的值将首先递增,然后赋值给x,而x的当前值将用于x++ +中的整个语句,然后x将递增,并用新值赋值。请看下面的例子-
#include<stdio.h>
void main()
{
int x=5;
/* Post & Pre-Increment used independently */
x++;
printf("x : %d", x);
++x;
printf("\nx : %d", x);
/* Used with printf() statement */
printf("\nPre-increment of x: %d", ++x);
printf("\nPost-increment of x: %d", x++);
printf("\nPost-increment effect on x: %d", x);
}
Output:
x : 6
x : 7
Pre-increment of x: 8
Post-increment of x: 8
Post-increment effect on x: 9
I Hope my explanation have made you understand, if still not you can reply me back.
我希望我的解释能让你明白,如果还没有,你可以回复我。
#3
5
The expression x + 1
should be x = x + 1
in for loop, so correct:
表达式x + 1在for循环中应该是x = x + 1,所以正确:
for(x = 0; x <= 100; x + 1)
^
doesn't increment x
Infinite loop!
无限循环!
as:
为:
for(x = 0; x <= 100; x = x + 1)
or
或
for(x = 0; x <= 100; x += 1) // or simply x++ or ++x
The compiler didn't produce such an error because x + 1
is a valid expression (but its not what you wants).
编译器不会产生这样的错误,因为x + 1是一个有效的表达式(但不是您想要的)。
So, x += 1
, x++
or ++x
are not just add one to x
but also modify value of x
.
所以,x+ = 1 x++ +或++x不仅仅是给x加1还可以修改x的值。
#4
1
Your approach is not perfect because it definitely increases the value of x but when it comes to assignment, it fails. so your statements should cover all your requirements.
你的方法并不完美,因为它确实增加了x的值,但是当涉及到赋值时,它就失败了。所以你的陈述应该涵盖所有的要求。
try to use
尝试使用
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x = x + 1)
printf("%d\t",x);
return 0;
}
or you can use
或者您也可以使用
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x++)
printf("%d\t",x);
return 0;
}
Instead of x++
(post increment) you can also use pre increment (++x
). The expression x = x + 1;
can also be written as x += 1;
which is a shorthand method for assignment embedded with increment. It is not just about incremnent you can also use other operators.
你也可以使用pre - increment (+ x)代替x++ (post increment)。表达式x = x + 1;也可以写成x += 1;它是嵌入增量的赋值的一种简写方法。您不仅可以使用其他操作符,还可以使用其他操作符。
x -= 3;
x *= 2;
#1
9
You need the value of x
to change, or your loop will never terminate. When you have only x + 1
, then at the end of the iteration, x + 1
is computed, but its resulting value is discarded. The result of the expression is not stored any place. So x
will remain zero, and x <= 100
will remain true, and your loop will continue forever.
您需要更改x的值,否则您的循环将永远不会终止。当只有x + 1时,在迭代结束时,计算出x + 1,但其结果值被丢弃。表达式的结果不存储在任何位置。所以x仍然是0,x <= 100仍然是正确的,你的循环会一直持续下去。
If you have any of the following:
如你有下列任何一项:
x = x + 1
x += 1
x++
++x
the value of x
itself is incremented. That's what you need.
x本身的值是递增的。这是你需要的。
#2
6
Although the basic structure of a for loop is like-
虽然for循环的基本结构是-
for(initialization; condition; increment or decrement)
But in its core part the "condition" part is treated only to control the loop behavior. So, if other two parts are syntactically correct, then compiler won't produce any error.
但在其核心部分,“条件”部分仅用于控制循环行为。因此,如果其他两个部分在语法上是正确的,那么编译器不会产生任何错误。
Since, x+1
is a valid C statement and the value of the x
is not changing so it will go to an infinite loop for the condition [x <= 100 => 0 <= 100
] being true forever.
因为x+1是一个有效的C语句,并且x的值没有变化,所以它会进入一个无限循环,条件是[x <= 100 => <= 100]永远为真。
Again, x++
or ++x
both treated as x = x + 1
when used independently. But, x++
is actually Post-increment operator, while ++x
is Pre-increment operator. That means, the with ++x
, the value of the x
will be incremented first and then assigned to x
. Whereas, the current value of x
will be used for the entire statement in x++
and then x
will be incremented and assigned with new value. Look at the following example-
同样,单独使用时,x++或++x都被视为x = x+ 1。但是,x++实际上是后增量运算符,而++x是预增量运算符。这意味着,++x的值,x的值将首先递增,然后赋值给x,而x的当前值将用于x++ +中的整个语句,然后x将递增,并用新值赋值。请看下面的例子-
#include<stdio.h>
void main()
{
int x=5;
/* Post & Pre-Increment used independently */
x++;
printf("x : %d", x);
++x;
printf("\nx : %d", x);
/* Used with printf() statement */
printf("\nPre-increment of x: %d", ++x);
printf("\nPost-increment of x: %d", x++);
printf("\nPost-increment effect on x: %d", x);
}
Output:
x : 6
x : 7
Pre-increment of x: 8
Post-increment of x: 8
Post-increment effect on x: 9
I Hope my explanation have made you understand, if still not you can reply me back.
我希望我的解释能让你明白,如果还没有,你可以回复我。
#3
5
The expression x + 1
should be x = x + 1
in for loop, so correct:
表达式x + 1在for循环中应该是x = x + 1,所以正确:
for(x = 0; x <= 100; x + 1)
^
doesn't increment x
Infinite loop!
无限循环!
as:
为:
for(x = 0; x <= 100; x = x + 1)
or
或
for(x = 0; x <= 100; x += 1) // or simply x++ or ++x
The compiler didn't produce such an error because x + 1
is a valid expression (but its not what you wants).
编译器不会产生这样的错误,因为x + 1是一个有效的表达式(但不是您想要的)。
So, x += 1
, x++
or ++x
are not just add one to x
but also modify value of x
.
所以,x+ = 1 x++ +或++x不仅仅是给x加1还可以修改x的值。
#4
1
Your approach is not perfect because it definitely increases the value of x but when it comes to assignment, it fails. so your statements should cover all your requirements.
你的方法并不完美,因为它确实增加了x的值,但是当涉及到赋值时,它就失败了。所以你的陈述应该涵盖所有的要求。
try to use
尝试使用
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x = x + 1)
printf("%d\t",x);
return 0;
}
or you can use
或者您也可以使用
#include <stdio.h>
int main(void){
int x;
for(x = 0; x <= 100; x++)
printf("%d\t",x);
return 0;
}
Instead of x++
(post increment) you can also use pre increment (++x
). The expression x = x + 1;
can also be written as x += 1;
which is a shorthand method for assignment embedded with increment. It is not just about incremnent you can also use other operators.
你也可以使用pre - increment (+ x)代替x++ (post increment)。表达式x = x + 1;也可以写成x += 1;它是嵌入增量的赋值的一种简写方法。您不仅可以使用其他操作符,还可以使用其他操作符。
x -= 3;
x *= 2;