Nested Else-Ifs Do Not Go Beyond The First Else-if

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HolyDoom Witch
    New Member
    • Oct 2012
    • 34

    Nested Else-Ifs Do Not Go Beyond The First Else-if

    Now here is a wonderful code I made, that tells your age the same as was on your last birthday TILL one day before of your birthday (so computer-like no? but I too happen to think that way when it comes to age!). But, as you can see, the nested else-ifs don't go beyond the first else-if level. And I think this SHOULD work, especially when the first nested else-if works. (Nutty C++.)

    One has to resort to if (condition && condition) to make it work.

    Now WeaknessForCats explained something about why it won't work but that was in the last thread which was a different topic where I happened to start all this. So please paste your reply here, then I'll respond.

    Though the point that should be taken here is that it won't work this way, and which is all that should matter.

    Thanks

    Code:
    string name;
    int day, month, year, b_day, b_month, b_year;
      cout <<"Enter your first name: "; cin >>name;
      cout <<"H! " <<name <<", what is the date today? Enter day (1 to 31): "; cin >>day;
      cout <<"Now enter the month (1 to 12): "; cin >>month;
      cout <<"And the year? (4 digits): "; cin >>year;
      
      cout <<"\nNow input your birth date the same way. Day: "; cin >>b_day;
      cout <<"Month: "; cin >>b_month;
      cout <<"Year: "; cin >>b_year;
    
    if (month < b_month) cout <<"\nYour age is: " <<year - b_year - 1 <<" as of today on "
                              <<day <<"-" <<month <<"-" <<year <<".";
    
    else if (month == b_month)
      { if (day < b_day) cout <<"\nYour age is: " <<year - b_year - 1 <<" as of today on "
                              <<day <<"-" <<month <<"-" <<year <<".";
      }
    else if (month == b_month)
      { if (day == b_day) cout <<"\nHey... you became " <<year - b_year <<" years old today on "
                               <<day <<"-" <<month <<"-" <<year <<"... Happy Birthday!!";
      }
    else cout <<"\nYour age is " <<year - b_year <<" as of today on "
              <<day <<"-" <<month <<"-" <<year <<".";
    Would have been nice if these site people could provide an interpreter too!
    Last edited by Niheel; Apr 5 '13, 07:15 AM. Reason: This thread is closed. Please be more respectful of members who have spent years contributing and helping. They are trying their best to understand you, which believe it or not after reading yours p
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It's not going to go to the next else if and check that condition because it only picks the first one where the condition is true.

    Just move the code in the second else if into the first.

    Comment

    • HolyDoom Witch
      New Member
      • Oct 2012
      • 34

      #3
      Well then, it can't be done, since that is not what is required: the first condition of the first else-if is not common to the every set henceforth.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        It can be done. The first else if condition is exactly the same as the second else if condition. It is only the nested if that has differing conditions. And that's fine, it will still work.

        Comment

        • HolyDoom Witch
          New Member
          • Oct 2012
          • 34

          #5
          Well yeah, that is common all right, but only in a three conditions, when day may be less, may be equal, or may be bigger than the birth day, and month equal to the birth month in all the three. But there will be a problem if today's month is BIGGER than the birth month. Because the first else-if condition implies that today's month be EQUAL to the birth month. Your style works till that point.

          What's more is, say even if this code had gotten through, then it would not be because the code can pass after every nested-multi-condition-else-if, in every program say if we used this style. Just till all the conditions happen to keep meeting, a nested-multi-condition-else-if code will work. Meaning this would be just by chance, if ever so happens. So ultimately, even if we happen to solve this fully, we still need to abandon the nested-multi-condition-else-ifs.

          Earlier WeaknessForCats had written that else-if is not an entity in C++, and else-if are written together because the spaces don't count. Well that may be true, but this is also true that still it works somewhat like a single operator. You are supposed to put an action after the "else" command, but after "else", "if" is smoothly accepted, and you can place an indefinite number of else-if blocks. And there are no errors. But looks like in a nested else-if, first else-if is the limit for C++, whereas in a straight else-if, it works perfectly with any number of else-ifs.

          Thanks

          Comment

          • HolyDoom Witch
            New Member
            • Oct 2012
            • 34

            #6
            I think "if (condition && condition)" is the only option to fulfill all the conditions in such a code. Plus it is going to work smoothly and remain short too. If you still think it is possible in the style your are saying Rabbit, then you might want to post the code itself. Though I think not.

            Thanks

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              Glad you got it working.

              Regarding your post #5, I can't say I understand what you're trying to say. It sounds like there's a misunderstandin g somewhere in there about the if structure.

              Comment

              • HolyDoom Witch
                New Member
                • Oct 2012
                • 34

                #8
                I am so sorry I did not test it, and your best answer had to be reverted.

                That post got edited; though I have explained more clearly what you did not understand.

                Well it suddenly struck me and I tested and there it was, not giving anything.

                You might want to edit your post too in accordance with the edits.

                Thanks

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  I still can't say I understand everything you're trying to say. But you certainly could use && to combine the conditions but it is not the only way to do so.

                  The following is pseudo code only to show the logic.
                  Code:
                  if (x==1) {
                     if (y==2) {
                     } else if (y==3) {
                     }
                  }
                  Code:
                  if (x==1 && y==2) {
                  } else if (x==1 && y==3) {
                  }
                  Both code blocks result in the same logic flow.

                  Comment

                  • HolyDoom Witch
                    New Member
                    • Oct 2012
                    • 34

                    #10
                    Okay. Here is the deal.

                    You test the code in the following way:

                    Consider that last month was your birthday and today is any date past your birth month. Now input the details in the program, and then see the result.

                    You do this step, and then you'll know what I am saying.

                    Thanks

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      Test what? You mean test your code? You can test your own code because I don't know what your code currently looks like.

                      You should be aware though that you don't account for the days after your birthday in your birth month in your code.

                      Comment

                      • HolyDoom Witch
                        New Member
                        • Oct 2012
                        • 34

                        #12
                        Okay then forget it.

                        As far as accounting for days after the birthday is concerned, then according to you, if someone is using such a program, and last month was his birthday, then no result would be fine, right? And by the way, why should someone account for days BEFORE his birthday if not after his birthday? Because, whatever days are there AFTER his birthday, are also the days BEFORE his NEXT birthday!!!

                        To anyone else who comes here, this topic is closed. Coding in such a style can never work unless it is something very simple with less conditions. If it has more conditions, then better to use some other operators instead of the NESTED else-if blocks, like 'if (condition&&con dition) action;'. Nested else-ifs just don't work in C++.

                        Thanks to everyone.

                        Comment

                        • Rabbit
                          Recognized Expert MVP
                          • Jan 2007
                          • 12517

                          #13
                          As far as accounting for days after the birthday is concerned, then according to you, if someone is using such a program, and last month was his birthday, then no result would be fine, right?
                          No, that's not what I said at all. My point is the opposite, no result would not be fine so you need to account for it.

                          And by the way, why should someone account for days BEFORE his birthday if not after his birthday? Because, whatever days are there AFTER his birthday, are also the days BEFORE his NEXT birthday!!!
                          Again you misunderstand my point. Your code doesn't account for when month is equal to b_month and day is greater than b_day. Which means your result will be incorrect when that scenario is reached.

                          If it has more conditions, then better to use some other operators instead of the NESTED else-if blocks, like 'if (condition&&con dition) action;'. Nested else-ifs just don't work in C++.
                          The question is not whether or not nested if's work. They work. Whether or not it is recommended is another question. It is recommended not to use nested if structures. But that wasn't your question. Your question was how to get nested if's to work even though in your previous thread it was recommended that you not use them.

                          Comment

                          • HolyDoom Witch
                            New Member
                            • Oct 2012
                            • 34

                            #14
                            No, that's not what I said at all. My point is the opposite, no result would not be fine so you need to account for it.
                            Then know that this is exactly what is happening when we use your style. You can't say a code is working because most conditions work, and one or two don't.

                            Again you misunderstand my point. Your code doesn't account for when month is equal to b_month and day is greater than b_day. Which means your result will be incorrect when that scenario is reached.
                            Why does not my code account for this? Which other scenarios can you think of in the last "else" that remains? So you completely ignored this "else". And that has been your problem throughout.

                            The question is not whether or not nested if's work. They work. Whether or not it is recommended is another question. It is recommended not to use nested if structures. But that wasn't your question. Your question was how to get nested if's to work even though in your previous thread it was recommended that you not use them.
                            It is not about RECOMMENDATION. The question was, to SEE if things can work this way FOR THIS PARTICULAR CODE, since this style of nested else-if CANNOT at all work past the first else-if, but since I might be forgetting something. Like I said in my opening post: "Though the point that should be taken here is that it won't work this way, and which is all that should matter." The word "recommendation " can apply only where the option is ALWAYS available. Here, this option can be had only when there are far too less conditions.

                            And that was already concluded.

                            Thanks

                            Comment

                            • donbock
                              Recognized Expert Top Contributor
                              • Mar 2008
                              • 2427

                              #15
                              The title of your post refers to "nested else-ifs" but the code you provided does have any nesting of else-ifs. I assure you that else-ifs do indeed work properly, whether nested or not.

                              The legs of an else-if cascade are mutually exclusive. No more than one leg will be executed. (If your else-if cascade ends with an else then exactly one leg will be executed.) The following two code snippets are equivalent:
                              Code:
                              if (A) {
                                 leg1();
                              } else if (B) {
                                 leg2();
                              } else if (C) {
                                 leg3();
                              } else {
                                 leg4();
                              }
                              Code:
                              if (A) {
                                 leg1();
                              }
                              if (!A && B) {
                                 leg2();
                              }
                              if (!A && !B && C) {
                                 leg3();
                              }
                              if (!A && !B && !C) {
                                 leg4();
                              }
                              The order of conditions in the else-if cascade establishes the priorities of the conditions. For example, if A is true then you're taking the first leg regardless of whether B or C are true or false.

                              The preceding discussion has been general and may not address your specific concern. Let's look at your specific program. You have three conditions:
                              1. your birthday has not yet occurred this year;
                              2. your birthday is today;
                              3. your birthday occurred sometime earlier this year.
                              These three conditions are mutually exclusive: there is no combination of dates for which more than one condition is true. These three conditions are also complete: there is no combinations of dates for which all of them are false. Because the conditions are complete, we only need to explicitly test two of the three conditions - we can rely on the else clause to catch the third condition.
                              Code:
                              if ([I]your birthday has not yet occurred this year[/I]) {
                                 cout <<"\nYour age is: " << year - b_year - 1 <<" as of today on " <<day <<"-" <<month <<"-" <<year <<".";
                              } else if ([I]your birthday is today[/I]) {
                                 cout <<"\nHey... you became " <<year - b_year <<" years old today on " <<day <<"-" <<month <<"-" <<year <<"... Happy Birthday!!";
                              else {
                                 // your birthday must have occurred sometime earlier this year
                                 cout <<"\nYour age is " <<year - b_year <<" as of today on " <<day <<"-" <<month <<"-" <<year <<"."; 
                              }
                              All that's left is to express the conditions:
                              Code:
                              if ((month < b_month) || ((month == b_month) && (day < b_day))) {
                                 // Your birthday has not yet occurred this year.
                                 cout <<"\nYour age is: " << year - b_year - 1 <<" as of today on " <<day <<"-" <<month <<"-" <<year <<".";
                              } else if ((month == b_month) && (day == b_day)) {
                                 // Your birthday is today.
                                 cout <<"\nHey... you became " <<year - b_year <<" years old today on " <<day <<"-" <<month <<"-" <<year <<"... Happy Birthday!!";
                              else {
                                 // Your birthday must have occurred sometime earlier this year.
                                 cout <<"\nYour age is " <<year - b_year <<" as of today on " <<day <<"-" <<month <<"-" <<year <<"."; 
                              }
                              The fact that these conditions contain || and && is due to the complexity of your conditions, not to any inherent limitation of else-if.

                              Everything I've said here is consistent with what Rabbit has said.

                              Comment

                              Working...