Thread: Newb question .. While loop and use of curly brackets on last else in loop

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Newb question .. While loop and use of curly brackets on last else in loop

    Hi All,

    I'm am new to C and was working on a binary search algorithm.

    The last else in the while loop has a statement wrapped in curly brackets and it compiles without errors given. But when i run the code it enters an infinite loop. When i remove the curly brackets it gives no errors and runs fine. I don't understand why it gets stuck in an infinite loop. Why is that? Any C experts maybe wanna explain more about this please?

    This is the piece of code:

    Code:
    bool search(int value, int values[], int n)
    {
        int first = 0;
        int last = n - 1;
        int middle = ( first + last ) / 2;
        
        while ( first <= last )
        {
            if ( values[middle] == value )
            {
                return true;
            }
            else if ( values[middle] < value )
            {
                first = middle + 1;
            }
            else
            {
                last = middle - 1;
                middle = ( first + last ) / 2;
            } // when removing these bolded brackets the code runs fine
        } 
        return false;
    }
    And for those interested in the piece of sorting code the above algorithm is merged with:

    Code:
    void sort(int values[], int n)
    {
           for ( int j = 0; j < n - i - 1; j++)
           {
               if ( values[j] > values[j+1] )
               {
                   int swap     = values[j];
                   values[j]    = values[j+1];
                   values[j+1]  = swap;
               }
           }
       }
        return;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well without explicit braces, what you have is
    Code:
            else
            {
                last = middle - 1;
            }
    
            // this happens EVERY time, regardless of if / else logic
            middle = ( first + last ) / 2;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    2
    Quote Originally Posted by Salem View Post
    Well without explicit braces, what you have is
    Code:
            else
            {
                last = middle - 1;
            }
    
            // this happens EVERY time, regardless of if / else logic
            middle = ( first + last ) / 2;
    Nice. I see now that's what kept it running.

    As for the curly brackets, would you say that the "last = middle - 1;" statement should be in brackets no matter what, even if it does run good without 'm?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by moonboots View Post
    Nice. I see now that's what kept it running.

    As for the curly brackets, would you say that the "last = middle - 1;" statement should be in brackets no matter what, even if it does run good without 'm?
    I would suggest you get into the habit of always using them, so you don't have to think too much.
    If braces are always present, you won't slip into the trap you fell into - namely adding a line of code completely changed the meaning of the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-20-2016, 08:58 PM
  2. IF Statement-- With curly brackets or semicolon
    By erdemtuna in forum C Programming
    Replies: 6
    Last Post: 01-10-2016, 06:40 AM
  3. newb loop question
    By filio623 in forum C++ Programming
    Replies: 10
    Last Post: 09-18-2009, 02:54 PM
  4. for ..loop newb question
    By mike71484 in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2006, 11:30 PM
  5. Newb needs help with loop issues
    By FatalError in forum C Programming
    Replies: 15
    Last Post: 09-04-2004, 02:19 PM

Tags for this Thread