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

if

<p>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.</p> <p>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.</p> <p>As discussed earlier, the conditional expression evaluates to a boolean value (true or false,) which in turn controls the flow of the program:</p> <p>If it evaluates totrue, control enters the if-block.</p> <p>If it evaluates to false, the if-block is by-passed.</p> <p>Looks complex, doesn't it? Nope! Here's the same thing in pseudo-code:</p> <p>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.</p> <p>If he's not coughing, skip all of that and carry on coding in Flash! </p> <p>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.)</p> <h2>Example</h2> <p>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:</p> <p>Q. “If you were to be born again as a musician, who would it be?” </p> <p>Q. “If you were to be born as an animal, what would it be?” </p> <p>Pseudo-code that mimics the above scenario in the Hero’s mind, would look like: </p> <p>var reincarnation:string = “…”;</p> <p>//some statements here</p> <p>if(reincarnation == “musician”) </p> <p>trace(“I’ll Be Bach”); </p> <p>if( reincarnation == “animal”) </p> <p>trace(“Next time, Duck”);</p> <h2>Note</h2> <p>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. </p>

Overview

We will learn about various conditionals related to ‘if’. The main focus is on the importance of brackets and conditionals such as if/else, if/else if…, nested if, if not etc.

04:14

Conditionals and Operators

Greater than the tutorials you'll find elsewhere, 02geek takes less time to teach how logical operators are directly linked to conditionals, and how conditional expressions can be formed using these o

04:49

if

We introduce the “if” construct and show how it can be used for making programming decisions while implementing the program logic

04:30

Brackets

We explain the importance of using brackets with if, mainly to combine a sequence of operations, all to be performed on the same condition

07:41

else

We introduce the language mechanism for providing alternatives in code; ie,. to make decisions when we have 2 options

07:29

Else-if: dealing with multiple conditions

We introduce the else-if construct, to help make logic decisions when more than 2 options are available

08:41

Nested if conditional construct

We introduce the nested if construct, useful when there are more than one criteria of decision making, including one inside another

09:08

Nested if’s – The Answer

We introduce another approach to nested if’s organization: mainly inverting the nesting levels of if constructs

04:44

Say it ain't so – the “If-Not” conditional

We introduce how to reorganize condition checking code using if constructs having Not (!) operators in the conditionals

02:38