Linear array search
Try to fix the task from the previous task so that it works correctly even if the required element is not in the array.
Hint: if the required element is not in the array, then it is necessary to exit the loop as soon as the array is out of bounds.
Note
It must be remembered that in C++ (as well as in Python, JavaScript, PHP) when using the logical connective
AND
(
&&
), if the first part is false , then the second part is not checked.
For example:
condition
a = 0 && b != 0
when
a = 5
, the first part of
a = 0
is false, so the second part of
b != 0
will not be checked by the compiler.