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

Nested if’s – The Answer

<p>A different approach to the</p> <p> “MBX channel Slide rendering scenario” </p> <p>We discussed in the last summary for nested-if constructs that we could apply them for 2 independent categories of checks for classification, labeling, or whatever weneed to automate. Since the criteria for categories are independent of each other, we could invert the levels of nesting. In plain language, it means that the condition(s) that we were checking inside the outer if-else clause are now promoted to the outer level, and vice-versa. </p> <p>In the context of the scenario of the previous summary, the MBX channel slide-rendering problem: we now first broadly check for the weather, and then classify the background color according to the actual time of day. </p> <p>The logic for this implementation of logic would look something like: </p> <p> “If the weather outside is Good, then</p> <p>If the time currently is daytime, then set background color to Sky-Blue. Otherwise, set the background to Pitch-Black.” </p> <p> “Otherwise, if the weather outside is gloomy, </p> <p>If the time is currently daytime, then set background to Gray-Blue. Otherwise, set it to Dark-Gray.” </p> <p>The pseudo-code for this application would look something like: </p> <p>if(weather == “good”) {</p> <p>if(time == “daytime”) {</p> <p>Background = “sky-blue”; </p> <p>}else {</p> <p>Background = “pitch-black”; </p> <p>}</p> <p>}else {</p> <p>if(time == “daytime”) {</p> <p>Background = “gray-blue”; </p> <p>}else {</p> <p>Background = “dark-gray”; </p> <p>}</p> <p>}</p> <p>Depending on the number of categories in each classification method, we can intuitively decide for ourselves which classification is nested outside, and which is surrounding the other. A good rule of thumb to make the code more readable, would be that “Have the category that has more conditionals on the outer level, and the other category (with fewer conditionals) nested inside the former.” But the decision is left to the programmer’s discretion, since the logic of the program could dictate otherwise. </p> <p>In general, there could be more than 2 categories to check for, so there is NO hard-and-fast rule or trick to find the “best” nesting order. But you will develop your own style and flare over time and with enough practice! </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