-
Chapter 4 discusses making decisions.
-
before we get into the two types of
-
decision structures or selection
-
structures, we need to understand what
-
a boolean expression is. A boolean
-
expression is a comparison. The results
-
of the boolean expression is going to be
-
either a TRUE or FALSE. Either the
-
expression is TRUE or the expression is
-
FALSE. These boolean expressions are used
-
in every selection structure.
-
The question that is being asked in
-
the diamond symbol of your flow chart,
-
that question is really a boolean
-
expression. Let's look at the first type
-
of selection structure. It's called a
-
dual alternative or binary, 'bi',
-
being two, because it provides an action
-
for each of the two outcomes.
-
If the expression, remember the expression
-
will be written inside the diamond of
-
your flow chart, if the expression
-
results to TRUE or YES, we follow this
-
plan or path of action. The alternative
-
is if the expression evaluates to FALSE
-
or NO then you follow a different plan
-
of action. In pseudo code a dual
-
alternative selection is often written as
-
an IF THEN ELSE statement.
-
The second type of selection structure
-
is the single alternative or unary
-
selection, single being one action or
-
path to follow. If the expression
-
evaluates to TRUE, you follow the path
-
and perform some sort of action. If the
-
expression evaluates to false then there
-
is no special action to take; we simply
-
exit the structure. You could write your
-
expression in such a manner that the plan
-
of action is executed from the FALSE
-
results but that's what we call negative
-
logic and we are going to discuss avoiding
-
that. So the action does not always have
-
to happen if the expression is TRUE but
-
you do want to be careful about how
-
you write your expressions. You want to
-
make sure your logic is clear and
-
understandable so sometimes that negative
-
logic can confuse people. The single
-
alternative selection when written in
-
pseudo code is often written as an IF THEN
-
statement. If the expression is TRUE
-
then do this. That's it. There's no else.
-
The IF THEN ELSE decision, remember this
-
is the dual alternative selection, the IF
-
line, if the condition is TRUE then the
-
THEN clause. The THEN clause holds the
-
action or actions that execute when the
-
tested condition in the decision is TRUE.
-
If your boolean expression evaluates to
-
TRUE you do the actions within the THEN
-
clause. If the boolean expression
-
evaluates to FALSE the THEN clause is
-
skipped and the actions in the ELSE
-
clause are executed.
-
Your boolean expression does not have to
-
be an 'equal' symbol comparison. You
-
don't have to write your boolean
-
expression as 'value1 = value2' and
-
remember we want to make sure that we
-
are talking about comparisons now, we
-
quite often use the '=' sign to do
-
comparisons. Don't confuse that with
-
chapter 2 when we discussed assignment
-
statements and the assignment operator
-
was also the '=' symbol.
-
These are comparisons; however, we are
-
not going to limit ourselves to just
-
'equal to' type comparisons. Most
-
programming languages support six
-
different types of comparison operators.
-
All of these comparison operators will
-
result in a TRUE or FALSE. You compare
-
the two values by using either variables
-
or constants or a combination of a
-
variable and a constant. The term
-
'trivial expression' is when you compare
-
two constants. In this example
-
the digit 20, 20 equals 20 that's always
-
going to be TRUE. It's never going to
-
change. So quite often in our
-
expressions we use variables because the
-
data in the variable changes. It's not
-
uncommon to compare a variable value to a
-
constant. It's not uncommon to compare
-
a variable to a variable and when I say
-
that I mean the data in one variable
-
compared to the data in another variable.
-
But it is very trivial to compare a
-
constant to a constant because a constant
-
never changes. Avoid comparing a
-
constant to a constant.
-
Here are the six relational operators
-
that our commonly accepted.
-
the equivalency operator or the equal to,
-
greater-than, less-than, greater-than or
-
equal-to, less-than or equal-to, and then
-
not-equal-to. Now once again I'm going
-
to say that depending on your language
-
when you actually write the code these
-
symbols may be slightly different
-
depending on the language.
-
Any logical situation can be expressed
-
with only the three types of comparisons,
-
equal-to, greater-than, less-than. So
-
technically we could argue that you could
-
throw out the greater-than equal-to and
-
the lesser-than equal-to, but that's kind
-
of a debatable discussion. Now here it
-
says it makes the code more readable.
-
I would argue that really depends on the
-
individual. If you want to say
-
Ms. Veral's video on boolean expressions,
-
she talks about asking the computer her
-
birth date. Is your birthday on or before
-
July 1st? Well if you want to be
-
inclusive, including July 1st, you could
-
just ask if it were before July 2nd.
-
That would include July 1st but not
-
include July 2nd. It's arguable whether
-
or not the less-than or equal-to, greater-
-
than or equal-to makes your code more
-
readable. Just know that we always have
-
the option that there's a work around
-
for these greater-than or equal-to,
-
less-than or equal-to.
-
And a not-equal operator, again, debatable
-
but I'm going to agree, probably the most
-
confusing of comparisons.
-
But the not-equal-to is asking if two
-
variables are not equal.
-
Here's some example flow chart and
-
example pseudo code using a negative
-
comparison. Is the customer code not
-
equal to 1. If the expression evaluates
-
to TRUE or YES, the customer code is not
-
1 and the discount is 0.25. If the
-
customer code not-equal to 1 evaluates to
-
FALSE, which think carefully that means
-
the customer code is equal to 1, then
-
the discount is 0.50. Here's that pseudo
-
code to match it. Customer code not
-
equal to 1 then discount is 0.25
-
otherwise discount 0.50. You can simply
-
rewrite this to be using positive logic
-
rather than negative logic. Instead of
-
saying not-equal-to. I want you to know
-
this before we change slides. Not-equal
-
to one, discount 0.25. Equal-to 1
-
discount 0.5. Notice that we're going to
-
keep the negative or the FALSE path on the
-
left and the TRUE path on the right.
-
But notice the question will change and
-
the actions will change.
-
Customer code is-equal-to 1, YES
-
discount 0.50. Customer code not-equal-to
-
1, discount 0.25. Look at the pseudo
-
code. Now I'm going to back up a slide.
-
Compare this pseudo code using straight
-
forward or positive logic, compared to
-
the same logic, excuse me, the same
-
actions but in negative logic. I would
-
argue that using the not-equal-to causes
-
a little bit more confusion. You are
-
certainly welcome to disagree.
-
Avoiding a common error with relational
-
operators, be very careful, read your code
-
do a desk check. I'm sorry, not your code.
-
Read your logic, walk through your pseudo
-
code, walk through your flow chart doing
-
a desk-check. Make sure you're using the
-
correct operators. A very common error
-
is missing that boundry or limit for a
-
selection. Again if we refer to the
-
birth date July 1st, you ask is your
-
birthday before July 1st. Well do you
-
want to include July 1st in your question?
-
That's where you've gotta ask. That's that
-
boundary or limit their talking about.
-
If I ask you to pick a number between 1
-
and 5. Between 1 and 5, does that mean
-
2, 3, or 4? Or are we being inclusive,
-
1, 2, 3, 4 and 5 pick one of those.
-
That's what we are talking about with the
-
boundary or the limit. So be careful
-
that you use the correct relational
-
operator in your boolean expressions.