Error using Else: "'Else' without a Previous 'if'"

Asked

Viewed 1,163 times

-1

I’m trying to make a basic program, but I stopped in half because it was wrong:

error:'Else' without a Previous 'if'

#include<stdio.h>
int main()
{
    int med1, med2, med3;
    printf("\nDigite a medida 1 : ");
    scanf("%f", &med1);
    printf("\nDigite a medida 2 : ");
    scanf("%f", &med2);
    printf("\nDigite a medida 3 : ");
    scanf("%f", &med3);
    if((med1+med2>med3) && (med2+med3>med1) && (med1+med3>med2));
    {
        printf("\n");
    }
    else;
    {
        printf("Medidas não formam um triangulo");
        return (0);
    }
    return(0);
}
  • You have placed a ; at the end of the if which terminated the command. Troque if((med1+med2>med3) && (med2+med3>med1) && (med1+med3>med2)); by if((med1+med2>med3) && (med2+med3>med1) & (med1+med3>med2)) Idem after Else.

  • And there’s another ; shortly after the else, also remove it

3 answers

2

You have several problems in your algorithm, I will point out only the syntax problems which refers to your question:

Line 16

error: 'else' without a previous 'if'

What problem are you accusing?

The else does not have a if.. That’s because you’re using character ; in the reserved word if and else, which is causing problems with the syntax when compiling the algorithm.

Let’s analyze your algorithm straight to error:

if((med1+med2>med3) && (med2+med3>med1) && (med1+med3>med2)); << REMOVA O (;)
{ 
    //código
} else; // << REMOVA O (;)
{ 
    //código
} 

In order for the compiler to understand the syntax of your algorithm, you can do this snippet of code in two ways, which are the following:

I) REMOVE THE CHARACTER ; AND RETURN INSIDE THE ELSE, AND ALIGNS CORRECTLY FOR GOOD PRACTICES THE KEYS {}

if((med1+med2>med3) && (med2+med3>med1) && (med1+med3>med2)) { 
   //código
} else {
   //código
}

II) REMOVE THE CHARACTER ;, AND SET UP A TERNARY OPERATOR WITH THE CONDITION STRUCTURE

(med1+med2>med3) && (med2+med3>med1) && (med1+med3>med2) ? printf("\n") : printf("Medidas não formam um triangulo");

NOTE:

TO BETTER UNDERSTAND HOW THE COMPILER OF A PROGRAMMING LANGUAGE WORKS, YOU MUST STUDY THE FOLLOWING CONTENTS:

  • Ling Automata. Formal and Computability
  • Compilers

0

You have mistakenly placed a ; right after the if condition and also right after Else. Declared its variables as int but uses the %f flag in the read, which is intended to float. Has a unnecessary Return (0) inside the Else block.

#include <stdio.h>
int main() { 
    float med1, med2, med3; 
    printf("\nDigite a medida 1 : "); 
    scanf("%f", &med1); 
    printf("\nDigite a medida 2 : "); 
    scanf("%f", &med2); 
    printf("\nDigite a medida 3 : "); 
    scanf("%f", &med3); 
    if((med1+med2>med3) && (med2+med3>med1) && (med1+med3>med2)) { 
        printf("\n"); /* Será que não deveria ter mais coisas aqui? */
    } else { 
        printf("Medidas não formam um triangulo"); 
    } 
    return(0); 
}

0

There’s a mistake in the if and in the else, these block commands (if-Else, for, while, do-while, switch-case.) do not carry the ;.

See your if and else:

if((med1+med2>med3) && (med2+med3>med1) && (med1+med3>med2));
{
    // código
}
else;
{
    // código
}

He should be like this:

if((med1+med2>med3) && (med2+med3>med1) && (med1+med3>med2))
{
    // código
}
else
{
    // código
}

Just remove those ;.

Edit.: Another important point, all code outside the block will be executed by default, in the case of yours else no need to put return 0; within it, for it will be executed afterwards. Imagine that there is more code after this conditional, it would simply not run, because with the return 0; you completed the execution of the program.

What is return 0;? It is an indicator to the computer that your program has been run successfully, ie without errors, the zero is a default value.

Browser other questions tagged

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