Silly question understanding a simple function

This is a discussion on Silly question understanding a simple function within the C++ Programming forums, part of the General Programming Boards category; Hello, all. I have this simple function in my book that I've utilized to search through a linked list. However ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    When you don't have brackets with a while, if, else, etc, then only one statement is part of that loop or block. So
    Code:
       while (here->data != target && here->link != NULL)
       here = here->link;
    is the same as
    Code:
       while (here->data != target && here->link != NULL)
       {
          here = here->link;
       }
    The same is true for the if and the else. Here is the same code with brackets added and indentation fixed to make it more readable:
    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;
          }
       }
    }

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Daved, thank you for that explanation it really makes sense to me now!
    Correct me if I'm wrong but what this function is it reaches the while loop and iterates until
    here->data == target. In that case it breaks the while loop, go to the if statement, returns TRUE.

    On the other hand, if it reaches the end of the linked list (here->link == NULL) it breaks
    from the whole loop, moves on to the if/else statement goes to ELSE and returns false.

    I seriously could not read that without the brackets, you are all the best =)

    Sincerely,
    T4yl0r

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> Correct me if I'm wrong
    No correction. You're exactly right. Glad I could help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Function name basic question help
    By kenryuakuma in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 07:48 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21