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

Else-if: dealing with multiple conditions

<p>Checking is not always just for a YES or a NO. When there are more than 2 possibilities to be checked by the program logic, a special language construct is required: the “else-if” clause. There are many examples of this, but the most common are when you have a particular variable in your program that can have multiple values, or you have ranges of values, with different action(s) to be taken for each range.</p> <p>In real life, we are often faced with situations where we need to have more than one option ready. Suppose the public (bus) transport can be used by giving change, and going from Brooklyn Bridge to Statue of Liberty in NYC costs half a dollar(a person can dream). Now, you could pay in cents, nickels, dimes or quarters, depending on what's jingling in your pocket. A passenger could reason:“If I have quarters, I give 2 of them. Otherwise if I have dimes, I give 5. Otherwise with nickles, 10 of them go. Otherwise if I'm loaded with pennies, out go 50. Else my butt is walking.”</p> <p>The pseudo-code for the above change example could be written as: </p> <p>if(denomination == “quarters”){ </p> <p>Change = 2Q; </p> <p>}else if(denomination == “dimes”){ </p> <p>Change = 5D; </p> <p>}else if(denomination == “nickels”){ </p> <p>Change = 10N; </p> <p>}else if(denomination == “pennies”){ </p> <p>Change = 50P; </p> <p>}else{</p> <p>assWalking = true; </p> <p>} </p> <p>Lol thanks perfect example :) </p> <p>The top-most if and subsequent else-if clauses check for specific conditions, or a specific combination of conditions. For the scenario where none of those conditions is met, the "else" clause is entered. The cascading else-if conditions must always be terminated by an "else" clause, for the “default” condition; i.e., if none of the conditions (if and else-if cascade) is true. </p> <p>As in the case of if/else alone, only one of the conditions (or only the default case) will evaluate to true, so control can only branch to one of the clauses in the cascade. Actually, the behavior of the program control depends on what conditions the user has passed, but language ensures only 1 block runs . </p> <p>Also, each else-if construct can have multiple operations, so curly brackets can be used for each clause in the cascade: </p> <p>else if(denomination == “pennies”){ </p> <p>Change = 50P; </p> <p>Busman = isPissed; </p> <p>}</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