-
Newbie Help
Please bear with me, for I just started learning C today.
I'm to the second lesson for C coding, I understand what's mostly going on, but this is bugging me. Are the {} brackets supposed to be at a certain point when you're converting over to an "else" statement?
Code:
#include <stdio.h>
int main()
{
int age;
printf( "Please enter your age" );
scanf( "%d", &age );
if ( age < 100 ) {
printf ( "Your are pretty young!\n" )
} /*I'm getting an error on this spot when I try to compile.*/
else if ( age == 100 ) {
printf( "You are old\n" ?;
}
else {
printf( "You are really old\n" );
}
return 0;
}
-
Code:
#include <stdio.h>
int main()
{
int age;
printf( "Please enter your age" );
scanf( "%d", &age );
if ( age < 100 )
{
printf ( "You are pretty young!\n");
}
else if ( age == 100 )
{
printf( "You are old\n");
}
else
{
printf( "You are really old\n" );
}
return 0;
}
Errors corrected above in blue, I have also made the indenting more consistent.
A { and } are needed whenever you have more than one statement after an if or else, otherwise only the first statement will be associated with it. It is a good habit to always use braces in an if, even if there's only one statement, so that if you decide to add another, you won't make the mistake of forgetting the { and }.
-
Okay, thanks alot. I don't know why I put a question mark there.
Darn those semi-colons.