开发者

for loop in C/C++

开发者 https://www.devze.com 2023-03-17 16:08 出处:网络
I have for开发者_如何学Go loop doubtthat I need to ask . once i saw in coding something like for(i = 0; i<10; i+)

I have for开发者_如何学Go loop doubt that I need to ask .

once i saw in coding something like

for(i = 0; i<10; i+)     

My doubt is why &when in for loop we use say i+ or i- rather than i++ or i--

Thanks in advance


It won't work, doesn't the compiler return an error if you do? ( or atleast a warning.. )

Just use ++i or i++


Using i+ instead of i++ should not work. As I think you know, i++ increases the value of i by one. When the compiler sees i+, it is expecting something after the +, which causes it to not compile.


The line in question is not valid C

for(i = 0; i<10; i+)

Some valid, equivalent (between themselves) options are

for(i = 0; i < 10; i++)
for(i = 0; i < 10; ++i)
for(i = 0; i < 10; i += 1)
for(i = 0; i < 10; ) { /*...*/ i++; }


You cannot use i+ or i- because if doesn’t work i.e doesn’t increase or decrease value of i for increment you have may use i++,++i,i+=1,i=i+1 and for decrement you can use these by changing sign to negtive

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号