0

I do not understand why I get an errors:

error: expected ';' after expression else (value > values[l])

and

helpers.c:40:21: error: expression result unused [-Werror,-Wunused-value] else (value > values[l])

on my code of binary search:

Returns true if value is in array of n values, else false. */ bool search(int value, int values[], int n) { // ensures that the n is non negative if (n < 1) return false; else { //making ints for binary searching: m - far right; k - far right ; l - middle/avarage of m and k; int m = 0; int k = n; int l = (m + k) / 2; do {

    if (l == 0)
    return false;

    else if (l == value)
    return true;

    else if (value < values[l])
    l = (m + (l - 1)) / 2;

    else (value > values[l])
    l = ((m + 1) + k) / 2;
    }while (value != values[l])
}

the errors if for else but to be honest I do not see the difference between else if and else. Can somebody explain it to me? Thanks.

1

There is no single thing called else if in the C language. The syntax of if is one of:

  • if ( expression ) statement
  • if ( expression ) statement else statement

The construct

if (condition1)
    single_statement1;
else if (condition2)
    single_statement2;
else
    single_statement3;

is the (almost exact) semantic equivalent of the code

if (condition) {
    single_statement1;
}
else {
    if (condition2) {
        single_statement2;
    }
    else {
        single_statement3;
    }
}

Notice that there is no else if - it is just that the else requires a single statement, which can be another if statement. We do indent it in code as if it was a chain, but to the compiler the other if is nested in an else.

Now, notice that the if gets the controlling expression - the condition - in parentheses - this is how the compiler can distinguish the condition from the statement that is guarded by the condition. An else block doesn't have a controlling expression in parentheses.

Your code has a syntax error in it. The compiler believes that the single statement for else should be (value > values[l]), but that doesn't end in a semicolon. Additionally, the value of that statement is an integer telling the result of comparison - but it is discarded, therefore you get the second warning. So the compiler parsed this code as if it were something like

// ... function prototype, do { ... 

        if (l == 0) {
           return false;
        }
        else {
            if (l == value) {
                return true;
            }
            else {
                if (value < values[l])
                    l = (m + (l - 1)) / 2;
                }
                else {
                    (value > values[l]) // except that there's a semicolon missing here!
                }
            }
        }
        l = ((m + 1) + k) / 2; // this gets grouped here...
    }
    while (value != values[l]) // another semicolon missing
}
| improve this answer | |
  • thank you so much! before CS50 'coded' a little in JS :) – koko loko Dec 11 '17 at 15:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .