My last post was about the use of ”if” as a condition in c++. Now I should complement that post with an explanation about ”else”.
When we use ”if”, we declare a condition. Then we make our program do something under that condition. But, what happens if the condition is not accomplished? Well, if we don’t use else, the program would end there. But, if we use else, we can make our program do some other thing when the condition we declared is not accomplished.
Here’s a code as an example:
#include <iostream>
using namespace std;
int hours;
int main()
{
cout<<“How many hours of study do you need to approve?”;
cin>>hours;
if (hours>5)
cout<<“You should start now”;
else
cout<<“You have some free time!”
return 0;
}
And now, to clarify a little bit more what “else” works for I made a short poem:
If else
If you do this,
I’ll do that.
If you say this,
I’ll say that.
If you take this,
I’ll take that.
We unconditionally
will state conditions,
not just in reality,
but in coding too.
What if?
Constantly questioning,
our head is
stuck in a single string.
We should open ourselves
to a second way,
where it’s not just about if’s
but also about else.
Else makes all clearer,
else makes all brighter.
”if”conditions are great, yes,
but complement them with else,
that’s even greater.
“If you don’t teach me,
you’ll see me failing.
Else, if you do,
you’ll see me nail it.”
Use of ”else” with a conditional by Mferflores is licensed under a Creative Commons Attribution 4.0 International License.