在IF语句条件内循环 [英] Loop within IF statement condition

查看:86
本文介绍了在IF语句条件内循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

样本:

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()函数从数据库中检索数据列表.该程序的目的是检查字符串是否包含数据库中的某些关键字.

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
}

我将代码更改为使用bool而不是int的代码,因为看起来您正在使用C ++.

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

这篇关于在IF语句条件内循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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