在IF语句条件下循环

时间:2011-06-21 01:15:40

标签: c++ qt4 if-statement

我只是想知道循环是否有一种方法可以在If语句条件下?

样品:

if((string.contains(stringlist.hello().value(0),Qt::CaseInsensitive))||(string.contains(stringlist.hello().value(1),Qt::CaseInsensitive))||(string.contains(stringlist.hello().value(2),Qt::CaseInsensitive)))
{
...
}

是:

if
(
for(int i=0; i < stringlist.hello().size(); i++)
{
string.contains(stringlist.hello().value(i),Qt::CaseInsensitive)
}
)
{
...
}

顺便说一下hello()函数从数据库中检索数据列表。 此程序的目的是检查字符串是否包含数据库中的某些关键字。

1 个答案:

答案 0 :(得分:8)

该代码无法编译;相反,您可以尝试一种解决方案,检查每个条件并将结果存储到一个变量中,以确定是否满足条件:

bool testCond = false;
for(int i=0; i < stringlist.hello().size(); i++)
{
    if (string.contains(stringlist.hello().value(i),Qt::CaseInsensitive))
    {
        testCond = true;
        break;
    }
}
if (testCond)
{
    // code here if any of the conditions in the for loop are true
}

我将我的代码更改为使用bool而不是int,因为它看起来像是在使用C ++。