如何使用两个或多个迭代器在for循环内递增 [英] how to use two or more iterators to increment inside a for loop

查看:316
本文介绍了如何使用两个或多个迭代器在for循环内递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int i,j;
for(i=0;j=10;j>=0;i<10;i++;j--){
   printf("%d %d",i,j);
}

执行时会带来错误,如何纠正错误以及在for循环中使用多个迭代器的正确语法是什么

It brings error while executing, how to rectify it and what is the correct syntax for using multiple iterators in for loop

推荐答案

for循环具有以下语法:

for ( expression ; expression ; expression )

有3个用分号分隔的表达式.您有6个用分号分隔的表达式.那是无效的语法.

There are 3 expressions separated by semicolons. You have 6 expressions separated by semicolons. That's invalid syntax.

您应将其编写如下:

for(i=0,j=10; j>=0 && i<10; i++,j--)

对于第一个表达式,请使用逗号运算符分隔两个赋值.对于第三个表达式也是如此.第二,您希望两个条件都为真,因此请使用逻辑AND运算符&&分隔它们.

For the first expression, separate the two assignments with the comma operator. Similarly for the third expression. For the second, you want both conditionals to be true, so separate them with the logical AND operator &&.

此外,您得到的错误不是在执行时而是在编译时.

Also, the error you got was not while executing but while compiling.

这篇关于如何使用两个或多个迭代器在for循环内递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆