简体   繁体   English

for循环中超过2个条件?

[英]More than 2 condition in a for loop?

Was wondering if there is a way to compare 3-4 conditions in a for loop? 想知道是否有办法在for循环中比较3-4个条件? Is there a way to do this with just a for loop or would I need to add an if statement within the loop to control not printing 91 - 96? 有没有办法只使用for循环或我需要在循环中添加一个if语句来控制不打印91 - 96?

I have tried adding an initial condition, brackets around both and around them all but cant seem to get it to print past 90 and pick up at 97. 我已经尝试添加一个初始条件,围绕它们和它们周围的括号,但似乎无法将它打印到90以上并在97处拾取。

for (int cntr = 65; cntr >= 65 && cntr < 91 || cntr > 96 && cntr < 122; cntr++)

for (int cntr = 65;  cntr < 91 || cntr > 96 && cntr < 122; cntr++)

Trying to print ASCII char for 65 through 122 without showing 90 through 96. Was trying to get it all in one for loop if possible. 尝试打印65到122的ASCII字符而不显示90到96.如果可能的话,尝试将其全部放在一个for循环中。

Ok, so fixed it like this: 好的,所以修复它是这样的:

for (int cntr = 65; cntr < 123; cntr++)
    {
        if(!(cntr >= 91 && cntr <= 96))
        cout << "#" << cntr << " = ASCII Char: " << char(cntr) << endl;
    }

Guess this is the best way to solve the issue for now. 猜猜这是解决这个问题的最佳方法。 Thank You again 再次感谢你

As others have pointed out, the central condition is a break condition. 正如其他人所指出的那样,中心条件是break状况。 As you want it all within one loop you don't want to break when you reach cntr == 91 . 如果您想要在一个循环内完成所有操作,则在达到cntr == 91时不希望破坏。

Here's a way to do it neatly in one loop (C++ style): 这是一种在一个循环中巧妙地完成它的方法(C ++风格):

for (char cntr = 65; cntr < 123; cntr++)
{
    if ( cntr == 91 ) // skips the parts that you don't want
        cntr = 97;

    std::cout << cntr << std::endl;
}

More generally, there are times when you might have multiple genuine break conditions in a loop, but it's best to keep some/most of them out of the first line of the for loop, ie. 更一般地说,有时候你可能在一个循环中有多个真正的中断条件,但是最好将它们中的一些/大部分保留在for循环的第一行之外,即。 for ( ... ; ... ; ... ) , as it's unnecessary and often is harder to read. for ( ... ; ... ; ... ) ,因为它是不必要的,而且通常更难阅读。 You can just write a break condition at the start of your loop (ie. line 2): 你可以在循环开始时写一个中断条件(即第2行):

for ( i = 0; break_condition_1; ++i)
{
    if ( break_condition_2 || break_condition_3 )
        break;

    // loop code
}

The answers above/below have explained it quite well about how you can go about having multiple for loop conditions. 上面/下面的答案很好地解释了如何使用多个for循环条件。

But say you are doing something like this (as suggested by Elliott Smith): 但是说你正在做这样的事情(正如Elliott Smith所说):

for ( i = 0; break_condition_1; ++i)
{
    if ( break_condition_2 || break_condition_3 )
        break;

    // loop code
}

It might be better to simply do this for clarity reasons: 为清晰起见,最好简单地执行此操作:

int i = 0;
while (break_condition_1 || break_condition_2 || break_condition_3){
   i++;
}

There is no major difference between for and while loops, aside from the fact for loop is neater to write in certain situations. for循环和while循环之间没有重大区别,除了循环在某些情况下编写更整洁的事实。 In your case, I would recommend the while loop. 在你的情况下,我会建议while循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM