Else without If

I was working on my If statements and watching a video that goes along with it. I am almost certain that my nested if statement is exactly identical to the tutor but his would run while mine would give the "else without if error". I have spent an hour going over what is wrong and even check different sites including this one but i was unable to resolve my issue. So what is my problem because the only thing i can refer to is that the version the tutor is on and the on i am on is different since he is on CodeBlocks 13.12 while i am using 16.01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include <stdio.h>
#include <stdlib.h>

int main()

{
      int age;
      printf("Please enter your age\n");
      scanf("%d",&age);/*scanf allows input and & before "age" is the address linking the integer*/
      if (age > 18){/*This if statement asks whether the "age is GREATER than 18 only*/
        printf("The age is greater than 18, ");/* The "printf" is the command that runs if the if statement is true*/
        } if (age < 21){
        printf("The age is greater than 18 but less than 21");
        }
        else {
            printf("The age is greater than 18 but no less than 21");
        }
        else if ( age == 18){/*This else if allows more options without overriding the before one*/
        printf("The age is equal to 18");
        }
        else {/*This else literally means if everything above does not apply*/
            printf("The age is less than 18");
        }/*I could have used another else if statement to do the same thing*/





    return(0);
Your code is missing an '}' at the end. Also, you can't have an else before an else if(...).

Take a look at your code again. Try to write your braces like this:

1
2
3
4
if(...)
{
    bla bla;
}


And Indent statements, that way, it's readable for me and you.
Last edited on
Think of "IF" as "Start Loop" and "ELSE" as "End Loop". "ELSE IF" would be more like "Continue".
The '}' that wasn't included at the end was there, but just wasn't in the range of what i was copying.
The guy that i watch was easily able to have an else before an else if and run normally
Is the video too old?
https://www.youtube.com/watch?v=-CpG3oATGIs&t=4803s
1:19:00
see if it makes more sense writing

if ()
{

}
else
{
if() //this is what those else-ifs really are. Its an illusion... this is what the
//language is allowing, and doing, masked by programmers being
//cute and putting everything on one line.
{

}
}

The guy that i watch was easily able to have an else before an else if 

In the video, the else at line 14 matches the if at line 11. The else if at line 18 matches the if at line 8. Note that the indentation in the video doesn't reflect the actual structure of the code.

If you use an automatic indenting program you'll see where the problem is. Here is your code after gnu indent tried to indent it to match the structure. Seen this way, it's clear that the else at line 18 doesn't match anything.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>

int
main()
{
    int age;
    printf("Please enter your age\n");
    scanf("%d", &age);				 /*scanf allows input and & before "age" is the address linking the integer */
    if (age > 18) {				 /*This if statement asks whether the "age is GREATER than 18 only */
	printf("The age is greater than 18, ");	 /* The "printf" is the command that runs if the if statement is true */
    }
    if (age < 21) {
	printf("The age is greater than 18 but less than 21");
    } else {
	printf("The age is greater than 18 but no less than 21");
    }
    else
if (age == 18) {				 /*This else if allows more options without overriding the before one */
    printf("The age is equal to 18");
} else {					 /*This else literally means if everything above does not apply */
    printf("The age is less than 18");
}						 /*I could have used another else if statement to do the same thing */

return (0);
}

So my problem was my formatting, writing ,and indentions or is there just nothing else to "else"
Last edited on
Formatting means nothing to the compiler. Its purpose is to allow the human reader to better understand the code. It's an aid to readability.

The problem might be expressed in two ways. Most simply, the code is incorrect since the 'else' does not match any previous 'if'. Another way to describe the problem is that the left and right braces { } are misplaced.
Here's the original code (still incorrect)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    printf("Please enter your age\n");
    scanf("%d",&age);
      
    if (age > 18) 
    {
        printf("The age is greater than 18, ");
    } 
    
    
    
    if (age < 21)
    {
        printf("The age is greater than 18 but less than 21");
    }
    else 
    {
        printf("The age is greater than 18 but no less than 21");
    }
    
    
    
    /* error here: following else is unmatched */
    else if ( age == 18)
    {
        printf("The age is equal to 18");
    }
    else 
    { 
        printf("The age is less than 18");
    }
    

    return(0);
    
}


Now this is what I think was intended. Note the nesting of one if inside another, using braces to control the scope.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    printf("Please enter your age\n");
    scanf("%d",&age);
      
    if (age > 18) 
    {
        printf("The age is greater than 18, ");
     
        if (age < 21)
        {
            printf("The age is greater than 18 but less than 21");
        }
        else 
        {
            printf("The age is greater than 18 but no less than 21");
        }
    }
    else if ( age == 18)
    {
        printf("The age is equal to 18");
    }
    else 
    { 
        printf("The age is less than 18");
    }
    

    return(0);    
}

Last edited on
your problem was already explained, but to answer: there is nothing else to else. You can start a new if statement after an else, is all.

Its on par with claiming you have a for-do loop because you can type

for(...) do

The reason this is more of an issue with if statements is that MANY other languages support an actual else-if statement. They need this due to their syntax and compiler and parser (you usually see it paired with languages that require an endif statement). C does not need it, because you can flow from one into another so easily.

Last edited on
Topic archived. No new replies allowed.