Q:

Swift program to check all array elements satisfy a condition

0

Here, we will create an array of integer elements. Then we will check a condition for all array elements using the allSatisfy() function. The allSatisfy() function returns Boolean value based on condition.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to check all array elements satisfy a condition is given below. The given program is compiled and executed successfully.

// Swift program to check all array elements
// satisfy a condition

import Swift

var arr:[Int] = [12,10,25,20,50]

var res = arr.allSatisfy {
    $0 < 60
}

print(res)

Output:

true

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift;

Here, we created an array arr that contains 5 integer elements. Then we checked a condition that all array elements are less than 60 or not using allSatisfy() function and print result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now