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
#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");
}
elseif ( 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);
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
}
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 elseat line 14 matches the ifat line 11. The elseifat line 18 matches the ifat 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 elseat line 18 doesn't match anything.
#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");
}
elseif (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);
}
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)
#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 */
elseif ( 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.
#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");
}
}
elseif ( age == 18)
{
printf("The age is equal to 18");
}
else
{
printf("The age is less than 18");
}
return(0);
}
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.