there is an error saying "illegal else without matching if"
how will i correct this one?
# include %26lt;iostream.h%26gt;
int main ()
{
// conditional statement
int age;
cout%26lt;%26lt;"Enter your age:";
cin%26gt;%26gt;age;
if(age%26lt;16)
cout%26lt;%26lt;"you are highschool.....";
cout%26lt;%26lt;endl;
else(age%26gt;16)
cout%26lt;%26lt;"Maybe you are college";
cout%26lt;%26lt;endl;
return 0;
}
About c++ (if and else)?
# include %26lt;iostream.h%26gt;
int main ()
{
// conditional statement
int age;
cout%26lt;%26lt;"Enter your age:";
cin%26gt;%26gt;age;
if(age%26lt;16){
cout%26lt;%26lt;"you are highschool.....";
cout%26lt;%26lt;endl;
}else{
cout%26lt;%26lt;"Maybe you are college";
cout%26lt;%26lt;endl;
}
return 0;
}
you dont need the else(age %26gt; 16), else means its going to be executed if the if statement is false and also you need
if(condition){
}else{
};
the { } are necessary is you have more than one line of code between if and else
if( condition)
one line
else
one line
is legal
Reply:You forgot the braces.
A if else statement's sytax is
if()
{
}else{
}
# include %26lt;iostream.h%26gt;
int main ()
{
// conditional statement
int age;
cout%26lt;%26lt;"Enter your age:";
cin%26gt;%26gt;age;
if(age%26lt;16)
{
cout%26lt;%26lt;"you are highschool.....";
cout%26lt;%26lt;endl;
}
elseif(age%26gt;16)
{
cout%26lt;%26lt;"Maybe you are college";
cout%26lt;%26lt;endl;
return 0;
}
Reply:if statements go like this
if (some conditional)
some expression
else
some expression
remember that to group a collection of expressions you need to put in { and }
so to make a BLOCK of code
if (some condition)
{
some code
some more code
}
else
{
even more code
a lot more code
}
Reply:You have two statements in the if block without curly braces.
if(age%26lt;16)
cout%26lt;%26lt;"you are highschool.....";
cout%26lt;%26lt;endl;
else(age%26gt;16)
cout%26lt;%26lt;"Maybe you are college";
cout%26lt;%26lt;endl;
should be:
if(age%26lt;16)
{
cout%26lt;%26lt;"you are highschool.....";
cout%26lt;%26lt;endl;
}
else(age%26gt;16)
{
cout%26lt;%26lt;"Maybe you are college";
cout%26lt;%26lt;endl;
}
Reply:try this:
int main ()
{
// conditional statement
int age;
cout%26lt;%26lt;"Enter your age:";
cin%26gt;%26gt;age;
if(age%26lt;16)
cout%26lt;%26lt;"you are highschool.....";
cout%26lt;%26lt;endl;
else
if(age%26gt;16)
cout%26lt;%26lt;"Maybe you are college";
cout%26lt;%26lt;endl;
return 0;
}
Reply:one line expression:
if()
... ;
else
... ;
more than one line expression:
if(){
... ;
... ;
}
else{
... ;
... ;
}
if(){
... ;
... ;
}
else if(){
... ;
... ;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment