-3

I was wondering about how the true/false values worked when inside If-Else statements? Specifically, a prompt asks to check if a certain year is a leap year. This is what I have so far:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main() {
   int inputYear;
   bool isLeapYear = false; //the prompt already sets bool isLeapyear = false

   cin >> inputYear;

   if (inputYear % 400 == 0) {
      isLeapYear = true;
   }
   else if ((inputYear % 4 == 0) && (inputYear % 100 != 0)) {
      isLeapYear = true;
   }
   else {
   }

   if (isLeapYear) {
      cout << inputYear <<  " is a leap year" << endl;
   }
   else {
      cout << inputYear << " is not a leap year" << endl;
   }
   return 0;
}

When running this, all the inputs check out, but I noticed that if the boolean variable is initially isLeapYear=false, then how is it that for the second if-statement

if (isLeapYear) {

if an input like 50 (which obviously is not a leap year) skips all the if/if-else/else statements and runs into this second if-statement, and if isLeapYear is never set to true since the input didn't meet any conditions, wouldn't the if-statement turn into if (false) ... and then should actually output "50 - leap year" ?

I essentially have the right code, but I'm confused on the logic as to why an incorrect/false input wouldn't also output to "is a leap year".

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 4
    I'm confused why you think `if (false)` should execute the `if` branch and not the `else` branch. `if` evaluates the expression in brackets, if it evaluates to `true`, the `if-statement` is executed, if it evaluates to `false`, the `else-statement` is executed (if it's there of course). – Yksisarvinen Jun 23 '23 at 07:29
  • 1
    `if (false) ... else ...` means the else branch is taken. For input 50 the output is "is not a leap year" – 463035818_is_not_an_ai Jun 23 '23 at 07:29
  • you can try this by writing code with `if (false) { std::cout <<"false"; } else { std::cout << "true"; }`. What would you expect to be printed? – 463035818_is_not_an_ai Jun 23 '23 at 07:31
  • `50` is not "incorrect / false" input. Its a totally valid `int`. `isLeapYear` is not modified, it stays `false`, hence you get `"is not a leap year"` as output – 463035818_is_not_an_ai Jun 23 '23 at 07:34
  • OT: you should have `isLeapYear = inputYear % 400 == 0 || ((inputYear % 4 == 0) && (inputYear % 100 != 0))` instead of an if/else. – Jabberwocky Jun 23 '23 at 07:35
  • 4
    When you have "complex" conditions in an if statement just make a small function. So the whole logic of `is_leapyear` should not be a boolean but a function `bool is_leapyear(const unsigned year)` then you can write all the logic in that function and your main will only have an `if (is_leapyear(year))`. The code is immediately more readable and better you can make tests for your function. – Pepijn Kramer Jun 23 '23 at 07:35
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Jun 23 '23 at 08:19

1 Answers1

1

if an input like 50 (which obviously is not a leap year) skips all the if/if-else/else statements and runs into this second if-statement, and if isLeapYear is never set to true since the input didn't meet any conditions, wouldn't the if-statement turn into if (false) ...

So far so good. Only a minor nitpick: The if/if-else/else is not "skipped". The onditions are evaluated they are both false, the else branch is taken (which is a noop).

You are right that if (leapYear) is equivalent to if (false) when isLeapYear was not assigned true.

.. and then should actually output "50 - leap year" ?

This is the puzzling part of your analysis. Consider this simpler example:

#include <iostream>


bool isLeapYear() { return false; }

int main() {
    if (false) { std::cout << "this is not printed"; } 
    else { std::cout << "Hello ";}

    if (isLeapYear()) { std::cout << "this is not printed"; }
    else { std::cout << "leap year!";}
}

It prints

Hello leap year!

In the first if statement if (false) implies that the else branch is taken.

In the second part I illustrated how you can use a function to evaluate the condition and return the result. As a first test, my isLeapYear always returns false. Hence if (isLeapYear()) is again equivalent to if (false) and the else branch is taken.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185