C++的循环问题
黄舟
黄舟 2017-04-17 13:43:07
0
6
525

在一个for循环体中,我需要用到两个变量。比如说:

for(int n=0;int f=n+1&&f<5;++n)
    cout<<f;

就像这种形式。我在vs2015上一跑就死循环了(。-_-。) 那这种情况下我应该如何改写代码让它能成功运行呢?
谢谢。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(6)
伊谢尔伦

First look at your code

for(int n=0;int f=n+1&&f<5;++n)
    cout<<f;
    

The intermediate loop termination condition is int f=n+1&&f<5, && has a higher priority, so it is equivalent to int f=(n+1&&f<5). Obviously, f is always 1. If you know the reason, you can see how to change it. You got it.

巴扎黑
在for 循环定义两个变量
for  (   int  a, b ;   ;  )

你的测试条件,没看懂
Peter_Zhu
for(int n=0f=0;f<5;++n) 

cout<<f;
f=n+1;
巴扎黑
    for (int n = 0; ; ++n){
        int f = n + 1;
        if (f >= 5) break;
        cout << f;
    }
左手右手慢动作
for (int n = 0, f = 0; (f = n + 1) && (f<5); ++n)
    cout << f;
黄舟

Thank you for your generous advice, I have found the answer. The reason is that I put an assignment statement where the conditional judgment is.
The Android client will not fix the problem, so I would like to thank you all here

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!