Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By EVOEx

break statement not within loop or switch error, cant figure out the problem, help!

This is a discussion on break statement not within loop or switch error, cant figure out the problem, help! within the C Programming forums, part of the General Programming Boards category; so this is my code, it's suppose to locate the first occurrence of value and return its position and return ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    9

    Post break statement not within loop or switch error, cant figure out the problem, help!

    so this is my code, it's suppose to locate the first occurrence of value and return its position and return -1 if not found. i dont understand why it's giving me this error, please help!

    Code:
    int list_search( itemType value, List L ) {
        if (L->count==0) {
                         printf("Warning: empty array!\n");
                          }
        else {
             int i;
             for (i=0; i<L->count; ++i);{
                 if (L->data[i]==value) {
                    return i+1;
                    break;
                 }
             }
             return -1;
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,821
    If you indented your code carefully, you might have spotted the problem:
    Code:
    int list_search( itemType value, List L ) {
        if (L->count==0) {
            printf("Warning: empty array!\n");
        }
        else {
            int i;
            for (i=0; i<L->count; ++i)
                /* do nothing in loop body */;
    
            {
                if (L->data[i]==value) {
                    return i+1;
                    break;
                }
            }
            return -1;
        }
    }
    In any case, the break would be unreachable since it comes right after a return statement.
    swifferclean likes this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    And another issue; you don't return anything for an "empty array".
    swifferclean likes this.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    9
    thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-05-2011, 11:55 AM
  2. switch-case,Infinite while-loop ,break
    By babu198649 in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2008, 08:12 AM
  3. Question about break in switch statement
    By alone2002dj in forum C Programming
    Replies: 1
    Last Post: 12-09-2007, 07:42 PM
  4. Break statement issues, how to return to start of loop?
    By brdiger31 in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2007, 03:29 PM
  5. break statement not within loop or switch
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-04-2006, 12:36 AM

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