Thread: Really newbie question

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    4

    Really newbie question

    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)

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    Re: Really newbie question

    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,

  3. #3
    Well first of all, This code obviously wont work.

    That opening brace screws everything up. You want (properly indented):

    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.";
    But secondly; Why not just try it out and see?

    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)

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Braces

    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.)

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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

  6. #6
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    if your coude were orginzied it would like this(not syaing my codes are organized)
    Code:
    if (sumthing == "frdgf")
      {
         if (something2 =="arggafgfd")
             {
                 cout << "hello";
             }
      }
    else if (gfegtr == "grege")
      {
        if (gvfsfd == "asfads")
           {
                cout << world";
           }
      }
    iftrees are the only organized thigns in my codes for that reason
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

  7. #7
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    Originally 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
    preludes a girl, i thouht he/she were a boy
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

  8. #8
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Originally posted by Klinerr1
    preludes a girl, i thouht he/she were a boy
    Very wrong =)

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM