Reading in English Week 3 Day 11 Theme: The Importance of Conditionals in Programming
Reading in English Week 3 Day 11 Theme: The Importance of Conditionals in Programming
The most known types of conditional statements are the IF..THEN, the IF..THEN..ELSE and
the CASE or SWITCH, although we could also mention the handling of exceptions as a more
modern alternative to avoid the "nesting" of conditional statements.
Conditional statements are, together with loops, the pillars of structured programming, and
their use is an evolution of assembly language statements.
A conditional instruction allows us to pose the solution to a problem considering the different
cases that may arise. In this way, we can use a different algorithm to face each case that
may exist in the world.
Consider the method of the Product class which is responsible for selling a certain number
of units present in the winery. There, two possible cases can be presented, eac h with a
different solution: the first case is when the quantity you want to sell is greater than the
quantity available in the warehouse (the order is greater than the availability) and the
second is when there are enough units of the product in the warehouse to make the sale.
In each of these situations the solution is different and the method must have a different
algorithm.
As explained above, there are two ways to alter the flow depending on a condition. The basic
idea is to find ourselves at a fork in the road, where we will follow one or the other depending
on the answer to a question written at the fork. A variant of this is that instead of two
possibilities, we are presented with more paths to follow.
Logical operators allow you to group logical expressions. Logical expressions are all
those expressions that obtain as a result true or false. Logical operators are those
that link this type of expressions. To create conditions, no matter how simple they
are, you need logical operators.
> means "greater than". If x>y, it means "if x is greater than y".
< means "less than". If x<y, it means "if x is less than y".
&& means "Y", the copulative conjunction; that is: If (x==y) && (x==z), means "if x is
equal to y Y x is equal to z.
|| means "O", the adverse conjunction; that is, If (x==y) || (x==z), means "if x is equal
to y O x equals z".
Operator and: The operator and evaluates if the value of the left side and the right side is
met.
False
True
Operator not: The operator not returns the opposite value to the boolean value.
False
If the expression is True the returned value is False, otherwise if the expression is False the
returned value is True.
True
References:
Megacursos.com
Universidad de los Andes, fundamentos de programación
Codenotch coding school S.L