1、break关键字用于 while、for 循环时,会终止循环而执行整个循环语句后面的代码。break 关键字通常和 if 语句一起使用,即满足条件时便跳出循环。这里强调一下,如果存在多层循环的时候,break只会跳出当前循环;下面是测试多层循环的情况
QList<int> listInt;
listInt << 2 << 8 << 9 << 10;
QList<int> tempList;
tempList << 2 << 10 << 8 << 9;
QList<int> newList;
for (int index = 0;index <listInt.count();index++) {
int tempInt = listInt[index];
qDebug() << "index:" <<index << "tempInt:" << tempInt;
for (int section = 0;section <tempList.count();section++) {
int sectionInt = tempList[section];
qDebug() << "section:" << section << "sectionInt:" << sectionInt;
if(tempInt == sectionInt){
newList.append(sectionInt);
qDebug() << "section相同";
break;
}
}
}
qDebug() << "newList:" << newList;
打印输出截图如下:
感兴趣的人,可以自行测试。