If we would have to teach a little boy about how to cross(walking) a street with traffic lights, it is not enough to tell him that he has to stand in one corner and cross to the other, which surely can cause him some injuries. Instead, we must teach him about the conditions that are necessaries to be met in order to cross the street, those conditions would be:
- If the green light is on, you must stop. Don’t cross, only cars can transit now.
- If the yellow light is on, continue waiting until the red one becomes on
- If the red light is on, you can walk now
With these instructions, the kid would be capable to cross the street at the right moment when all the necessary condition is met.
In a similar way, in a program, we can instruct devices about the conditions that must be met in order to execute certain actions. In the case of crossing a street a valid algorithm would be:
The program must be capable to verify the conditions that are necessaries in order to execute certain groups of instructions, that verification is done through the use of conditionals and logical operators.
In order to be validated, conditions must be expressed in such a way that its result can only be true or false, like formulating a yes/no question. This can be done using comparison operators, like equals (=), greater than(>), less than(<), among others.
If the given condition returns true as a result, then the program would execute some set of instructions, in the opposite case, when it returns false the program would execute another set.
Each set of instructions is known as branch
The most simple way to write a conditional uses an IF statement, a condition to evaluate and a set of instructions to execute when the condition is met.
In this case, the condition will be evaluated and only if its result is true the instructions will be executed. If the condition returns a false value the program will continue its regular flow without executing the instructions inside the conditional.
In some cases, we also want our programs to execute some instructions when the given conditions return a false value. This could be achieved using an ELSE statement associated to our IF.
In this case, the condition will be evaluated and only if its result is true the instructions inside the IF block will be executed. If the condition returns a false value then the instructions inside the ELSE block are executed.
In some other cases, we might want to evaluate different conditions and execute different sets of instructions on each case. This could be achieved using an ELSE IF statement associated to our IF.
Here, the condition will be evaluated and only if its result is true the instructions inside the IF block will be executed. If the first condition returns a false value, then our program will evaluate the condition in the ELSE IF block, if it returns true then the instructions inside the block are executed. If both the conditions in the IF block and in every associated ELSE IF return all false values then the instructions inside the ELSE block are executed.
We can use multiple ELSE IFs, but only one IF and one ELSE in a single block
We can compare a conditional with a fork intersection; in which, for a very strange reason, green cars must take the left path and cars of any other color, the right one. In this case, the condition could be expressed in the following way:
In the example above, the condition to be validated is the car’s color = green, in the cases where this condition is true the set of instructions “A” will be executed, and in the other cases will be the set of instructions “B” the one executed.
Let’s see another similar example, suppose there is a three-lane road with some restrictions:
- If the vehicle is driving to the X city and it is less than 3 tons heavy, then it must take lane 1
- If the vehicle is going to X city and it is 3 tons heavy or more, it must take lane 2
- If the vehicle is going to Y city it must take lane 3
We are in front of 3 possible routes that allow us to go to one of 2 cities (x or y), but in addition, there is one of the routes that doesn’t support heavy vehicles, so it has an additional weight restriction. As we can see, in order to transit through lane 1 there are 2 conditions to validate, and the same way in lane 2, but for the 3rd lane there is only necessary that our destiny would be the Y City.
In order to evaluate more than one condition in a single instruction, we must use logic operators. These operators take 2 or more conditions and indicate to us if conditions satisfy or not the operator’s restrictions, by returning a true or false value as a result.
Most known logic operators are AND and OR, the restrictions of each are the following:
In the case of AND operator, we can note that it only returns true when all the conditions are true, in the other hand OR operator returns true when at least one of the conditions are true.
Suppose again that we have another fork intersection with 3 possible routes, where route 1 can be taken in order to go to the X city by cars of less than 3 tons of weight, route 2 lead us also to X city but only can be taken by cars of 3 or more tons of weight and route 3 lead us to Y city.
Another commonly used operator is the NOT operator, this simply negates the value returned by any expression, so if it is true when we apply the NOT it’ll become false, and when it returns false it’ll become true
- In programming, there is the possibility to verify some conditions and execute a certain set of instructions or another depending on its result.
- The result of the evaluation of conditions only can be true or false
- It can be verified more than condition at the same time by using logic operators
- AND operator returns true only when all the conditions are true
- OR operator returns true when at least one of the conditions is true.