David1083
David1083

Reputation: 33

Multiple conditions in an If else statement c program

My program is supposed to basically determine the type of triangle based on the length of the sides that the user inputs.
I'm getting this error message for my code which tests to see if it's a scalene triangle

expected ';' before '{' else((right != left) || (right != bottom) || (left != bottom)){

Code:

else((right != left) || (right != bottom) || (left != bottom)){
                printf("This is a scalene triangle");
            }  

The error is saying to put a ; right after the last condition, which doesn't make sense to me. I tried doing so to test it out but it gives me the wrong answer.

Upvotes: 1

Views: 2019

Answers (2)

freefaller
freefaller

Reputation: 19963

I'm assuming this is supposed to be an if...else in which case you need an extra if after the else...

else if ((right != left) || (right != bottom) || (left != bottom))
{
    printf("This is a scalene triangle");
}

In response to the OP's comments...

From the notes i were reading the format was the last statement of the else if just being else, hence why I didn't use else if and just used else

The notes are correct, to a point - in that the last statement can be an else statement (and if you have an else it must be the last statement and there can only be one).

So the following is valid...

if (a == 1) {
   // Do this
} else {
   // Do that
}

But the following is not valid...

if (a == 1) {
   // Do this
} else {
   // Do that
} else {
   // Do other
}

The else if allow you to continue the logic processing over multiple blocks... which can finish with an else block if required...

if (a == 1) {
   // Do this
} else if (b == 1) {
   // Do that
} else {
   // Do other
}

Or not...

if (a == 1) {
   // Do this
} else if (b == 1) {
   // Do that
} else if (c == 1) {
   // Do other
}

Upvotes: 5

Nerdy
Nerdy

Reputation: 1099

else cannot have a condition statement, it is supposed to be else if if condition check need to be included.

It must be changed as below

else if ((right != left) || (right != bottom) || (left != bottom)){
                printf("This is a scalene triangle");
            }  

Upvotes: 1

Related Questions