The code itself "works" as in it doesn't bring any errors, but it isn't working as intended. What I was hoping to do was to get it to say something like:
I have 3 dollars
I have 2 dollars
I have 1 dollar
Instead however, it does this:
I have 3 dollars
I have 3 dollar
I have 2 dollars
I have 2 dollar
I have 1 dollar
I tried adding break; after line 6, but then it stops at "I have 20 dollars" and it doesn't continue.
1 2 3 4 5 6 7 8 9 10 11 12
int main() {
for (int i = 20; i > 0; i--) {
if (i > 1) {
std::cout << "I have " << i << " dollars\n";
}
else (i == 1); {
std::cout << "I have " << i << " dollar\n";
}
std::cout << "'one day later...'\n";
}
else is binary, what I mean is you have if this, then that, else other. That is all you get, ever. You can CHAIN the statements, but you can't do more.
to chain them:
if(something)
code;
else if(something else)
other code
else
default code;
which for you simply means putting the keyword IF after your else on line 8, before the condition (which does nothing currently, and beware of that ; on the end of it, that has to go away)
currently what it really says is
if(i>1) then cout
else do nothing
always cout again.
the new logic changes it to
if(i>1)
cout
else
if (i == 1)
other cout
note that some languages have elseif keywords. C++ does not, the else and the next if are distinct statement/code blocks, but I personally feel else if (condition) all on one line looks nice.