DEV Community

Steve Tomi
Steve Tomi

Posted on

A Noobs question

As I am made to understand, a For loop continues to run until it's condition turns false

Now am faced with an Array on which I want to perform a linear search on

int numbers[] = {2, 3, 7, 9, 1, 0, 5};

For (int i = 0; i < 7; i++)
{
if (numbers [i] == 7)
{
printf ("Found");
return 0;
}
}
printf ("Not Found");
return 1;

As I get to understand how the code works, it brought up a question

If 7 is found in the third iteration, will the For loop continue to run until the condition turns false, or it will stop when it sees a return value of 0
I ask this so I can be able to better place the second printf function

Top comments (2)

Collapse
 
ac000 profile image
Andrew Clayton

return causes an immediate return from a function, regardless of its value. So yes, in the above code, the for loop will not execute beyond the 3rd iteration.

Collapse
 
steffqing profile image
Steve Tomi

Thank you