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

if

In the previous summary, we discussed what a conditional is and how it is constructed. An if construct is the fundamental decision-making feature of all programming languages. With it we declare a condition – a fork in the road, a decision point for our programs.

In real life when we ask a question we expect an answer. For example if you ask someone “Did you go to school?” You expect a yes/no result. Similarly if is the most common way of asking questions when it comes to programming.

As discussed earlier, the conditional expression evaluates to a boolean value (true or false,) which in turn controls the flow of the program:

If it evaluates totrue, control enters the if-block.

If it evaluates to false, the if-block is by-passed.

Looks complex, doesn't it? Nope! Here's the same thing in pseudo-code:

if the cat starts coughing – do a bunch of things: push him off the bed, yell for your girlfriend to come take care of him, and run like hell.

If he's not coughing, skip all of that and carry on coding in Flash!

Also, no alternatives to the “entry/bypass mechanism” are considered -- no options such as “what to do otherwise”, because there is no “otherwise”. This construct can be used only for simple condition testing, without any other alternative provided. (He's either coughing or he's not – there's no in between.)

Example

Consider, for example, everyone’s favorite Last Action Hero being interviewed about his choice of reincarnation based on questions posed by his “die-hard” fans:

Q. “If you were to be born again as a musician, who would it be?”

Q. “If you were to be born as an animal, what would it be?”

Pseudo-code that mimics the above scenario in the Hero’s mind, would look like:

var reincarnation:string = “…”;

//some statements here

if(reincarnation == “musician”)

trace(“I’ll Be Bach”);

if( reincarnation == “animal”)

trace(“Next time, Duck”);

Note

Notice here that this piece of code needs to be put in ourmain() function to be called for execution. There can be as many stand-alone “if” statements as the programmer wants to put in the code. This can be verified by the reader by writing a simple main() function with the above code embedded. This simple condition testing lays the foundation for decision making in computer programs.

Video Transcript

00:00So let's see how we build our conditional,
00:02our fun, fun conditional,
00:04which is gonna give us so many powers
00:06when we're going down the road and building applications.
00:10It's kind of one of those stepping stones
00:11that are so critical,
00:13such as the variables that we've been playing around
00:16so far with.
00:17So the syntax is quite simple,
00:19and basically it starts with if,
00:22that's our first conditional,
00:24and then we're gonna open up a round bracket,
00:26we're gonna put a condition inside of that,
00:29open the bracket,
00:30we're gonna put a condition,
00:31you can see on the highlighting right here.
00:35After the condition,
00:36we're gonna close our bracket,
00:39then open a curly bracket,
00:41and close a curly bracket as we can see right here.
00:44I like just setting it up immediately
00:46before I even start writing the condition,
00:48just making sure that you've got your brackets in there.
00:52Brackets are one of those tricky things
00:53that you could, by mistake,
00:55forget to put one of them and then you're in trouble.
00:58So let's put our first condition,
01:00and our first condition could be,
01:02we could just take our is morning,
01:06and you might be asking me,
01:07but Ben, this is not a condition,
01:09and that would be a really good,
01:12you know, you caught me,
01:13you completely caught me,
01:14because it isn't the condition,
01:15but because a condition is not really mandatory
01:19in an if statement,
01:21but a Boolean value is,
01:23because in the end, for example,
01:25this is a condition,
01:26but what does this condition produce?
01:29It produces a Boolean value
01:30that then we could store into a Boolean,
01:32or we could feed into our if statement.
01:35So if we would ask your if is morning,
01:38it's, or less, the same thing
01:41as just setting up a simple if statement, okay?
01:46So now that we've talked about that,
01:47I'm gonna delete that,
01:48and let's build a really kind of classical,
01:50kind of take it step by step,
01:53and build it out until we get into that variable again.
01:56So I've deleted it.
01:57So we start with, again,
01:58we say we open with an if statement,
02:01and then we open a round bracket,
02:03close a round bracket,
02:04open a curly bracket, enter, enter,
02:06put there another curly bracket,
02:08and we have our structure all set.
02:10In our if statement,
02:11now we wanna put condition.
02:13So let's ask if it's later than six,
02:16or six or later in the morning,
02:18like we've done earlier.
02:20So we would type in I time of day,
02:25large or equal to six.
02:26So far, so good,
02:27and we've met that before,
02:28and we know how to do that.
02:31Now, let's maybe, let's tinker with this.
02:33Maybe instead of large,
02:34maybe we wanna ask if it's anything but the morning.
02:37How would we do that if we didn't use our Boolean value?
02:41So if it's anything but the morning,
02:43then it has to be,
02:45time of day needs to be smaller than six,
02:48or time of day needs to be larger or equal to 12, right?
02:51So how do we do or?
02:53We've learned that as well.
02:54So we're gonna put our two pipes.
02:57After we put our two pipes,
02:59we could ask again,
03:00I time of day is larger or equal to 12.
03:08Okay, so we have here all our range,
03:09and the number we could get is,
03:10or between zero and six,
03:14or 12 and onwards, okay?
03:18So let's, if it's not morning,
03:20let's trace out a message,
03:22and let's trace out a message,
03:24and we're gonna say, not good morning to you.
03:30So not good morning to you.
03:31Now, if we run the application right now,
03:34do you think that we're gonna see the message,
03:37not good morning to you?
03:38And the answer would be, no.
03:43The reason is because it is morning right now,
03:45and our condition here is basically saying,
03:46if it's smaller than six,
03:48if our time is smaller than six,
03:50or larger or equal to 12.
03:52And that's not true in this scenario at all.
03:56So if we run our application now one more time,
03:58we're gonna get our trace of true,
04:01but we're not gonna get our trace
04:03of not good morning to you.
04:05Let me just delete that trace
04:06because we don't need that trace.
04:09But if we would change the time of day now
04:11to be anything but morning,
04:13such as 24, or not 25,
04:16then there's a thing that's 25,
04:18let's use 13, which is 1 p.m.
04:22And we run the application,
04:23we're gonna see not good morning to you.
04:25And we hit the nail in the head.

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!