Flow control

where P is the probability that hostile aliens will come in a year and x is the number of years you have waited. So you plug these numbers and for the first year ( in this case 0) you get 0.336%. Phew, we’re safe for now. But you wonder for a bit “Is there a way for me just to run the program and if the percentage is above 50 it will display some warning message?”. By now you probably realized that the answer is yes! If a program needs to run different parts of the code depending on a condition, that’s where we need flow control. It’s rather simple as it is very similar to the previous sentence. Flow of control in C++ is realized through something called if-else statements. The general structure of the statements is as follows:
if (condition)
{
//Do something if condition is true
}
else if (second_condition)
{
//do something if condition is false, but second_condition is true
}
else
{
//do something if both condition and second_condition are false
}
Just as a side note: one might think that the conditions must be of the type boolean. That is not the case because as we will see in the next lecture, C++ implements certain types of type conversions. To keep it short, any numerical value (not equal to 0) will be converted to true, but if it evaluates to 0, it will be converted to false. Now, let’s get back to our example.
In this example, there are two conditions, condition and second_condition. If the condition happens to be true, the only block of code that gets executed is the one right below it, if not, second_condition is checked. If it happens to be true, great – it’s block gets executed, but, in the case where both conditions are false, the else block gets executed. You can also have more than one else if statement, but we think you get the point. Just as a heads up, you might not need an else block. Speaking of which, we still haven’t solved our little alien problem. Let’s try writing the code:
By running this program, we see that we are safe, which means that the probability is surely under 50. As an exercise, try to find the smallest value of “number_of_years” for which the probability goes above 50 so that we have time to make tin-foiled hats for the whole team here at Wipher. You can comment your result at the end of this lecture.
Ternary operator
Imagine you want to assign a value to a variable, but its assignment is dependent on some condition. Let’s look at how we can do that in the next example:
int x;
bool condition = ...; //Some condition
if (condition)
{
x = 34;
}
else
{
x = 23;
}
We see that the logic of this is rather simple (there is only one single instruction in each block), but the actual implementation (the amount of code we need to write) is rather big. Luckily, there is an operator implemented in C++ which can shorten the amount of code we need to write to a single line! The operator is called the ternary operator. Its functionality is simple: If some condition is true, parse the expression given by the first argument. If it is false, parse the second expression given by the second argument. A general structure is given in pseudocode:
(condition) ? first_expression : second_expression;
The condition is given before the question mark and the expressions are given after the question mark, which are separated by a single colon. Let’s look at our previous example and try to write it with the ternary operator:
int x;
bool condition = ...; //Some condition
x = condition ? 34 : 23;
As we see, we are able to do the whole assignment in a single line of code!
Table of contents
- Loops, Pointers and Functions
- Classes and Objects
- Operator Overloading
- Class Inheritance
- Exceptions
- Namespaces
- Templates
- Standard Library
- Additional Problems