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