Silly question understanding a simple function
Hello, all.
I have this simple function in my book that I've utilized to search through a linked list. However HOW it works is really stumping me, I would be very grateful if someone can elaborate as to why this function does what it does:
Code:
bool search(NodePtr head, int target)
{
NodePtr here = head;
if (here == NULL)
{
return false;
}
else
{
while (here->data != target && here->link != NULL)
here = here->link;
if (here->data == target)
return true;
else
return false;
}
}
First of all I don't understand why there are no brackets after while and until the end of the funtion or in hte second if/else statement, but I can only come to the assumption that this is legal since it works.
In the scenario where here->data == target, how the heck would it return true? Is the subsequent if statement OUTSIDE of the loop? If the if/else statement is outside of the loop, how can it decide for each here->data whether it is true or not.
I'm really confused, and kinda noobish :(
Sincerely,
T4yl0r