02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Conditionals and Operators

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);

Video Transcript

00:01Before we delve into conditionals, which
00:03is our main digital topic, we have to get a few.
00:09Well, let's get the concept.
00:10What is a conditional?
00:11And I'm sure you've met a lot of conditions in your life.
00:14And although it's in programming,
00:18it's the same type of environment where the application reaches
00:22a point and you want to be able to do one thing when
00:26one scenario matches up and another thing
00:30when a different scenario matches up,
00:32such as if in the real life you would have breakfast
00:36in the morning and lunch during midday and dinner at night.
00:42There's a few conditions here where in the condition
00:44of morning, you're going to have breakfast.
00:46In the condition of lunch, you're going to have lunch.
00:48In the condition of dinner, you would have dinner.
00:51And that's really abstract and quite simple.
00:53And it's not that different in programming.
00:56So there's just a syntax that we have to get used to.
00:59And we've already met this syntax more or less.
01:02And we're just missing a few little elements.
01:04And before we start digging into it,
01:08I just want to do a really, really, really quick recap
01:11of Booleans, because Booleans connect to conditions
01:14very, very closely.
01:16As a Boolean value is basically a flag.
01:18It's a point where there's true or false.
01:22And when something is true or false,
01:24you're going to have a condition where you could ask maybe
01:26when it's true, do this.
01:29No, when it's not true, do that.
01:31So for example, when we created a Boolean,
01:33and let's call this is day.
01:39And it's going to be a Boolean.
01:41And we could put into our Boolean value.
01:44And for example, before we do it,
01:47let's create a variable before Boolean, which will be,
01:49let's say, time of day, which would be an integer.
01:53We'll just be the hour.
01:55Time of day, which would be a UN,
01:59because it could only be a positive value.
02:02And let's say the time of the day is,
02:04we're working on an army clock, which is from 0 to 24.
02:08And the time now is 8 in the morning.
02:12And we could even call our Boolean is morning.
02:19So let's ask our Boolean, is it morning time?
02:22How would we ask our Boolean, is it morning time?
02:24I will not ask it.
02:25How could we put into our Boolean value?
02:29That option.
02:29So let's go under the assumption that from 6 in the morning
02:33to 12 PM is morning time.
02:36Let's first ask our Boolean, is it
02:40greater than 6 in the morning?
02:42So is our I time of day, or I time of day,
02:49is I time of day bigger than 6?
02:55And it's true, because I time of day in our scenario
02:57is bigger than 6, because it's later than 6.
03:00Right now it's 8.
03:01But that's not enough to consider it as morning, right?
03:04So we have to add another option here.
03:05So as we've learned in a previous course,
03:08we could say if time of day is bigger than 6,
03:12end end I time of day is smaller than 12.
03:23Now if we wanted to say that the morning time is also
03:256 in the morning, it's not only 6 in the morning,
03:27we could have changed the 6 to 5, because then it would be,
03:31if I time of day would be 6, it would be valid.
03:33If it would be 5, it wouldn't.
03:35Or we could say greater or equal.
03:38So if we recap this, here we're checking
03:40to see if I time of day is larger or equal to 6,
03:45while in this end we're checking
03:46to see if I time of day is smaller than 12.
03:49And because we're using an end end,
03:51we're basically saying if I time of day
03:53is greater or equal to 6, end time of day
03:56is smaller than 12.
04:00Thus, in that scenario, our time of day is morning.
04:02So if we trace this value out right now,
04:05and trace out is morning, we should
04:09be getting a value of true.
04:12And we're getting a value of true.
04:14Well, if we change the time of day
04:16to be some other time, such as let's say 8 p.m.,
04:19which would be 20 in army time,
04:22and we run the application again, we'd
04:23get the value false, because 8 p.m. is not the morning.
04:26We got the Boolean definitions kind of nicely locked.
04:30And if not, I really recommend pause this course
04:33and go back to Booleans, get Booleans really,
04:36really tightly understood.
04:38Play around with the conditionals,
04:39go back to the conditional course videos,
04:42and get the conditionals, the operators,
04:45really, really in your head before
04:47you move into the conditionals.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!