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
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:55 PM
  2. switch-case,Infinite while-loop ,break
    By babu198649 in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2008, 09:12 AM
  3. Question about break in switch statement
    By alone2002dj in forum C Programming
    Replies: 1
    Last Post: 12-09-2007, 08: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, 04:29 PM
  5. break statement not within loop or switch
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-04-2006, 01:36 AM

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