Error in using Else

Asked

Viewed 895 times

0

The program is giving an error when compiling.

Else without a Previous if

Something about the else, there’s something wrong with the condition if and else?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char est_civil;

    printf("Digite seu estado civil: ");
    est_civil = getchar();

    if (est_civil == 'C' || est_civil == 'c');
        printf("Casado");
    else

        if (est_civil == 'S' || est_civil == 's');
            printf("Solteiro");
        else

    return 0;
}

2 answers

3

  1. The ifs shall not have a semicolon in the end

  2. The second else is empty. Needs to be removed.

The code can still be simplified, but the syntax errors are these.

The only thing I think is important to change is to use one else if instead of creating a block else with if in. This increases somewhat the readability of the code, it might be interesting to see this publication*.

Something like:

if (est_civil == 'C' || est_civil == 'c')
    printf("Casado");
else if (est_civil == 'S' || est_civil == 's')
    printf("Solteiro"); 

Corrected code.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char est_civil;

    printf("Digite seu estado civil: ");
    est_civil = getchar();

    if (est_civil == 'C' || est_civil == 'c')
        printf("Casado");
    else if (est_civil == 'S' || est_civil == 's')
        printf("Solteiro");

    return 0;
}

*Writing easy-to-maintain, readable code?

  • I got it, I went soft because I put point and comma in the if and Else condition, and there is no haha. Now I ran the program.

  • From what you’re saying, it seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

  • Voce knows that "use Else if" and "create an Else block with an if inside" are exact the same thing, it’s not?

  • Yes, that’s why I said I would change. Why? That’s not clear?

2

So it works, and much simpler, no?

#include <stdio.h>

int main() {
    printf("Digite seu estado civil: ");
    char est_civil = getchar();
    if (est_civil == 'C' || est_civil == 'c') {
        printf("Casado");
    } else if (est_civil == 'S' || est_civil == 's') {
        printf("Solteiro");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Never stop putting keys on if, unless you know what you’re doing, which depends on a lot of experience. Especially don’t put the ; at the end of the if because it is closing it and nothing else will run as part of its block, it becomes innocuous except by side effect, which is advanced to its stage, and almost no one uses it even when it can make any sense.

The if is a command block. Blocks should be placed between keys. Even though they may eventually be omitted, and there are cases that can, so the compiler does not prevent it, it should not do this to avoid unexpected errors. Without the keys you incur dangling Else.

When you’ve got nothing to do in one else, do not use it. When what you have to do right after a else but a if, make a else if and create a single block.

See more in What happens if I don’t specify the { }?.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.