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 use different blocks of code according to different scenarios.
Let’s start!
Conditions
Conditions can be of different types, but in the end they all come down to TRUEs or FALSEs. 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 to
The 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
.
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 to TRUE if the all the conditions are met at the same time.||
: The OR operator; it evaluates to TRUE if 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 > valueB
This 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.
If you got here, well good job!
These lessons may be a bit boring, but are very important to understand how the Bitcoin Code works.
See you soon!
Tuma
I was so excited seeing this email in the inbox this morning!