检查数组中的所有值

Checking all values in an array

本文关键字:数组 检查      更新时间:2023-10-16

我有一个这样的数字数组:

int array[5];

我正试图打破一段时间的循环。我基本上想说如果数组中的所有数字都大于 100,然后结束循环。

有没有办法处理它?

您可以检查数组中的每个元素,如果只有元素小于 100,则中断。

在循环外部,根据i - 循环计数器 - 是否等于大小来打印消息。如果等于所有元素都大于,否则其中一个元素较小:

int array[] = {271, 120, 469, 77, 2000};
//int array[] = {271, 120, 469, 787, 2000}; // uncomment this line and comment out the line above to see the difference.

int i = 0; 
for(; i < 5; i++)
    if(100 > array[i])
        break;
if(i != 5)
    std::cout << "Not all the values are greater than 100" << std::endl;
else
    std::cout << "All the values are greater than 100" << std::endl;