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

Nested if conditional construct

<p>If execution depends on multiple different conditions (including one inside the other: the inner depends on the outer one)</p> <p>Until now we focused on one scenario at a time. In the real world and in programing many times we do different things depending on more then one factor. If you stop and ponder over this one, you'll notice that many times throughout the day conditions overlap each other.</p> <p>For example, in the MBX news channel, they just created a new slide that dynamically shows users an image based on a few parameters that overlap. There are a total of four possible colors the slide can be rendered in: black, dark gray, gray-blue and vivid blue. During the day: if the weather is great, the background image for the sky is blue. In any other case it is gray-blue. At night: if the weather is great the sky background is black and in every other scenario the sky background is rendered dark gray.</p> <p>This situation occurs frequently in practice, when there are 2 or more categories of conditions to be checked, on the same entities.</p> <p>In this context, the pseudo-code for the weather program scenario would be:</p> <p>var time:string = “…”; var weather:string = “…”; var render:string = “…”;</p> <p>//populate the above strings using logical actions</p> <p>if(time == “daytime”) {</p> <p>if(weather == “good”) {</p> <p>render = “sky-blue”; </p> <p>}else {</p> <p>render = “gray-blue”; </p> <p>}</p> <p>}else {</p> <p>if(weather == “good”) {</p> <p>render = “pitch-black”; </p> <p>}else {</p> <p>render = “dark-gray”; </p> <p>} </p> <p>}</p> <p>trace(“rendering MBX slide as” + render); </p> <p>Nesting of an if-else clause inside other clauses is very intuitive, used in day-to-day actions you do yourself. The most important aspect you should grab and latch onto is always trying to find the relationship between the real world and programming. At the end of the day, programs were created for humans (some might say programmers are sub-human but yet we are human!</p> <p>As such all programing languages were created tobalance between making things easy for both the computer and humans to understand.As long as you're open to noticing it, programming will become easier and mainly big fun!</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