开发者

Loop within IF statement condition

开发者 https://www.devze.com 2023-03-14 10:57 出处:网络
i just want to know if there is a way for a loop to be on the If statement condition? sample: if((string.contains(stringlist.hello().value(0),Qt::CaseInsensitive))||(string.contains(stringlist.hello

i just want to know if there is a way for a loop to be on the If statement condition?

sample:

if((string.contains(stringlist.hello().value(0),Qt::CaseInsensitive))||(string.contains(stringlist.hello().value(1),Qt::CaseInsensitive))||(string.contains(st开发者_如何学编程ringlist.hello().value(2),Qt::CaseInsensitive)))
{
...
}

to be:

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

by the way hello() function retrieves list of data from the database. the purpose of this program is to check a string if it contains some keywords from the database.


That code will not compile; instead, you might try a solution that checks each condition and stores the result into a variable determining whether or not the condition is met:

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
}

I changed my code to use bool instead of int's, as it looks like you are using C++.

0

精彩评论

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