Hello everyone! I really hope you are enjoying your free time!
In case you are feeling bored, feel free to deep dive into this new episode of Learn2Code.
Today, our side quest in the world of coding brings us to familiarize with conditional clauses. What are they? How do they work? Why are they important for programmers?
Let’s dive in!
Introduction
When programming, it is important to be able to execute some parts of the code only if a certain condition is satisfied. Someone might like to perform an action if something happens and another one if it doesn’t.
To be able to do so, if clauses are used in C++. This type of syntax allows the programmer to run different blocks of code according to different scenarios.
Conditions
Conditions can be of different types, but in the end they all come down to true or false. When a condition is set, the aim is to compare different values and variables
Here are some examples:
var1 > var2 // greater than
var1 < var2 // lesser than
var1 == var2 // equal to
var1 != var2 // different toThe symbols >, <, == and !=, that are derived from math, are called operators and return true if the condition is met or false if it’s not. The next code blocks shows a small example:
int var1 = 5;
int var2 = 10;
bool res = var1 < var2;What do you think will be the value of res? In this case the < operator will return a true value, since var1 is lesser than var2.
Consider supporting this newsletter by using one of my affiliate links. These are not sponsorships, just products I use everyday. Thank you!
Logical operators
What if the programmer wanted to check more than one condition? For example, he may want to check if a certain number is smaller than a value but at the same time greater than another one.
Logical operators come to the rescue! These operators allow to concatenate different conditions, in order to evaluate the most complex cases. There are three logical operators:
&&: The AND operator; it evaluates totrueif the all the conditions are met at the same time.||: The OR operator; it evaluates totrueif at least one condition is satisfied.!: The NOT operator; it negates the conditions to which it is applied.
If we wanted to check if a certain number is smaller than a value but at the same time greater than another one, we may do like this:
var1 < valueA && var1 > valueBThis condition evaluates to true if and only if both controls return true. On the contrary, if just one of them returned false, all the condition would evaluate to false.
IF clauses
To finally evaluate the conditions, an if clause must be used; the keyword if has to be declared, followed by the condition that should be met. The block of code inside the if clause will be executed if and only if the overall condition evaluates to true. Here’s a brief example:
int var1 = 5;
int var2 = 3;
if (var1 > var2) {
std::cout << “The first number is bigger than the second one”;
}Since var1 is actually bigger than var2, the condition inside the if clause will evaluate to true. Thus, the block of code between parentheses will be executed.
If more than one condition has to be evaluated, the keyword else can be used. In particular:
else if: allows the programmer to check for another condition;else: executes the code if none of the previous conditions is met;
For example:
int var1 = 5;
int var2 = 3;
if (var2 > var1) {
std::cout << “The first number is bigger than the second one”;
}
else {
std::cout << “The second number is bigger than the first one”;
}In this case the first condition evaluates to false; thus, the second block of code will be executed.
Let’s keep in touch:
Check out my writings on btc++ insider edition
Try my new app Sats Tracker, an expense tracker app for people living in the Bitcoin standard.
Zap me a coffee and leave me a message: tuma@wallet.yakihonne.com



I was so excited seeing this email in the inbox this morning!