
In our introduction to Conditionals, we saw how we use conditional expressions in real life. Now, a machine needs to be explicitly programmed, in a programming language to make such decisions. In this summary, we shall elaborate on the concepts of logical expressions and operators, which together form conditional expressions. Conditional expressions are heavily used in if-else constructs.
A conditional is an entity which has a “Boolean value”, namely true or false. A conditional is constructed with logical expressions and operators. The operators used are of two types:
• Comparison Operators:
Perform arithmetic comparisons; namely, <, >, =, !=(not equal), <= >=
• Logical Operators:
Perform logical operations; namely, && (and), || (or) and ! (not)
A Logical Expression is built using identifiers (variables), constants, comparison operators and other logical expressions.
A Conditional is composed of 1 or more logical expressions, strung together with 0 or more logical operators.
It's time to get a bit personal with a story from long ago. In 1984 Mary had a little lamb (by the way his name was Bobby) and she had a best friend,Ann. Ann was a farmer as well and had 4 lambs. It's a really long story that we won't get into, but actually Mary and Ann made a pact or as Mary said to Ann in the early bad hair days of the 80's : “Ann you know I love you but I needto go to Holland until 2014. When I come back if you have more lambs then me then we will eat one of yours.If I have more then you,mine will be on the table.”
Yes, a very touching story–the girls didn't speak to each other again – but there is an “if” statement lurking in this tale...
The pseudo code for the above example’s reasoning would look like:
var maryLambs:integer = 1;
var annLambs : integer = 4;
// process logic for 30 years
var eatMaryLamb:boolean = false;
var eatAnnlamb:boolean = false;
eatMaryLamb = (maryLambs > annLambs);
eatAnnLambs = (maryLambs <= annLambs);
Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.