If, Else if, Else Help!

This is a discussion on If, Else if, Else Help! within the C++ Programming forums, part of the General Programming Boards category; Yes I know your code compiles with those commas in there, and yes I knew it would appear to work, ...

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    Yes I know your code compiles with those commas in there, and yes I knew it would appear to work, but no it still does not do what you think it does.
    Had you changed it to the following, and entered 50 when prompted, you would have then noticed that it doesn't do what you thought:
    Code:
    else if( age >=4100, age <= 60 ) {
    I.e. no number can be both greater than 4100 and less than 60, yet it will still go into that if statement, illustrating my point.


    Quote Originally Posted by BMJ View Post
    I think this is what you actually intended...
    Code:
    if (age <= 40) {
       cout << "You are pretty young!";
    }
    else if (age<= 60) {
       cout << "You are kind of old!";
    }
    else {
       cout << "You are very old!";
    }
    Yep, and there we have it; Unnecessary parts removed.

    Good to know I'm still pretty young!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Registered User arc_angel14's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    18
    Quote Originally Posted by yosimba2000 View Post
    ok thx BMJ, so ELSE does not have any arguments right? it just stays as else.
    yea hes right since it just means everything else

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21