can you have more than 1 if else statement in an if statement
ie.
if(x==100)
{
cout << "you are old.";
else if(x<=11)
cout << "you are young.";
else if(x<19 && x>=12)
can you have more than 1 if else statement in an if statement
ie.
if(x==100)
{
cout << "you are old.";
else if(x<=11)
cout << "you are young.";
else if(x<19 && x>=12)
Originally posted by SumeragiSubaru
can you have more than 1 if else statement in an if statement
ie.
if(x==100)
{
cout << "you are old.";
else if(x<=11)
cout << "you are young.";
else if(x<19 && x>=12)
Yes,
Well first of all, This code obviously wont work.
That opening brace screws everything up. You want (properly indented):
But secondly; Why not just try it out and see?Code:if (x==100) cout << "you are old."; else if(x<=11) cout << "you are young."; else if(x<19 && x>=12) cout << "you are something else.";
And FYI (not sure if you understand the whole concept, apologies if you do), you can have else without else if:
Code:if (x==100) cout << "you are old."; else if(x<=11) cout << "you are young."; else if(x<19 && x>=12) cout << "you are youngish."; else cout << "you are some age or other.";
"There's always another way"
-lightatdawn (lightatdawn.cprogramming.com)
It looks like there may be some confusion with the curly-braces...
If you want to execute more than one expression or statement based on the results of 'if' or 'else', you tell the compiler to group the statements together by using a pair of braces. The code will execute if you ALWAYS put the conditional code inside braces. (But, people will ridicule you if you put a single statement inside braces following an if-statement.)
DougDbug,
Some people may ridicule, not all. Excluding the curly braces from a single statement following an 'IF' is perfectly acceptable, but it doesn't make it "better" code.
Now to the bigger issue, SumeragiSubaru.
This is a cross-post.
Prelude already addressed the nested 'ELSE...IF', question with you. In fact, she was quite clear in her explanation and I'm confident that she would have taken the time to explain it differently if you had indicated that you didn't understand.
If this was an inadvertent error on your part, you can edit/delete your post. Otherwise, please, don't do this. It's not good form.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
if your coude were orginzied it would like this(not syaing my codes are organized)
iftrees are the only organized thigns in my codes for that reasonCode:if (sumthing == "frdgf") { if (something2 =="arggafgfd") { cout << "hello"; } } else if (gfegtr == "grege") { if (gvfsfd == "asfads") { cout << world"; } }
preludes a girl, i thouht he/she were a boyOriginally posted by skipper
DougDbug,
Some people may ridicule, not all. Excluding the curly braces from a single statement following an 'IF' is perfectly acceptable, but it doesn't make it "better" code.
Now to the bigger issue, SumeragiSubaru.
This is a cross-post.
Prelude already addressed the nested 'ELSE...IF', question with you. In fact, she was quite clear in her explanation and I'm confident that she would have taken the time to explain it differently if you had indicated that you didn't understand.
If this was an inadvertent error on your part, you can edit/delete your post. Otherwise, please, don't do this. It's not good form.
-Skipper
Very wrong =)Originally posted by Klinerr1
preludes a girl, i thouht he/she were a boy
Klinerr1,
Don't be hard on yourself. I thought so, too, for some time.
Still have a spot on the wall to repair. "Prelude's a girl"...thump. "Prelude's a girl"...thump.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow